Guest User

Untitled

a guest
Jul 21st, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.14 KB | None | 0 0
  1. from __future__ import print_function
  2.  
  3. import json
  4. import os.path
  5. import buck_write
  6. import moc_rules
  7.  
  8. # TODO - linkFlags
  9.  
  10. SRC_DIR = '/Users/ilyaklyuchnikov/ovrsource/'
  11.  
  12. SHARED_TGS = []
  13.  
  14. START = """# BUILD FILE SYNTAX: PYTHON_DSL
  15.  
  16. load(
  17. "//Tools/buck:configure_file.bzl",
  18. "configure_file",
  19. )
  20.  
  21. load(
  22. "//Tools/buck:add_builtin_configs.bzl",
  23. "add_builtin_configs",
  24. )
  25.  
  26. command_alias(
  27. name='moc',
  28. exe = "//third-party/qt/qt_5.10.0:moc",
  29. )
  30. """
  31.  
  32. OVERRIDEN = {
  33. 'TelemetryConfig': {
  34. 'target.rule': 'configure_file',
  35. 'target': 'TelemetryConfig',
  36. 'input': 'Software/CoreTech/src/Libs/Telemetry/TelemetryConfig.h.in',
  37. 'output': 'TelemetryConfig.h',
  38. 'header_namespace': '',
  39. 'defs': {},
  40. },
  41.  
  42. 'BuildFlags': {
  43. 'target.rule': 'configure_file',
  44. 'target': 'BuildFlags',
  45. 'input': 'Software/CoreTech/cmake/BuildFlags/BuildFlags.h.in',
  46. 'output': 'BuildFlags.h',
  47. 'header_namespace': '',
  48. 'defs': {},
  49. },
  50.  
  51. 'BoltConfig': {
  52. 'target.rule': 'configure_file',
  53. 'target': 'BoltConfig',
  54. 'input': 'Software/CoreTech/bolt/CompileDefinitions.h.in',
  55. 'output': 'CompileDefinitions.h',
  56. 'header_namespace': '',
  57. 'defs': {},
  58. },
  59.  
  60. 'ConstellationTrackingConfig': {
  61. 'target.rule': 'configure_file',
  62. 'target': 'ConstellationTrackingConfig',
  63. 'input': 'Software/CoreTech/src/Engines/Tracking/ConstellationTrackingConfig.h.in',
  64. 'output': 'ConstellationTrackingConfig.h',
  65. 'header_namespace': '',
  66. 'defs': {},
  67. },
  68.  
  69. 'viper_core_CompileDefinitions': {
  70. 'target.rule': 'configure_file',
  71. 'target': 'viper_core_CompileDefinitions',
  72. 'input': 'Software/CoreTech/viper/viper/core/CompileDefinitions.h.in',
  73. 'output': 'CompileDefinitions.h',
  74. 'header_namespace': 'viper/core',
  75. 'defs': {},
  76. },
  77.  
  78. 'RecordingCompileDefinitions': {
  79. 'target.rule': 'configure_file',
  80. 'target': 'RecordingCompileDefinitions',
  81. 'input': 'Software/CoreTech/src/Management/Recording/RecordingCompileDefinitions.h.in',
  82. 'output': 'RecordingCompileDefinitions.h',
  83. 'header_namespace': '',
  84. 'defs': {},
  85. },
  86.  
  87. 'AthenaVersion': {
  88. 'target.rule': 'configure_file',
  89. 'target': 'AthenaVersion',
  90. 'input': 'Software/CoreTech/src/Loaders/Athena/version.h.in',
  91. 'output': 'version.h',
  92. 'header_namespace': '',
  93. 'defs': {},
  94. },
  95.  
  96. 'Athena2Version': {
  97. 'target.rule': 'configure_file',
  98. 'target': 'Athena2Version',
  99. 'input': 'Software/CoreTech/src/Loaders/Athena2/version.h.in',
  100. 'output': 'version.h',
  101. 'header_namespace': '',
  102. 'defs': {},
  103. },
  104.  
  105. 'TestConfig': {
  106. 'target.rule': 'configure_file',
  107. 'target': 'TestConfig',
  108. 'input': 'Software/CoreTech/cmake/TestConfig/TestConfig.h.in',
  109. 'output': 'TestConfig.h',
  110. 'header_namespace': '',
  111. 'defs': {},
  112. },
  113.  
  114. 'ResourcesInterface': {
  115. 'target.rule': 'configure_file',
  116. 'target': 'ResourcesInterface',
  117. 'input': 'Software/CoreTech/src/Libs/Visualization/ResourcesInterface.h.in',
  118. 'output': 'ResourcesInterface.h',
  119. 'header_namespace': '',
  120. 'defs': {},
  121. },
  122.  
  123. 'OptimizerCompileDefinitions': {
  124. 'target.rule': 'configure_file',
  125. 'target': 'OptimizerCompileDefinitions',
  126. 'input': 'Software/CoreTech/optimizer/CompileDefinitions.h.in',
  127. 'output': 'CompileDefinitions.h',
  128. 'header_namespace': 'optimizer',
  129. 'defs': {},
  130. },
  131.  
  132. 'MixedrealityCompileDefinitions': {
  133. 'target.rule': 'configure_file',
  134. 'target': 'MixedrealityCompileDefinitions',
  135. 'input': 'Software/CoreTech/mixedreality/mr/core/CompileDefinitions.h.in',
  136. 'output': 'CompileDefinitions.h',
  137. 'header_namespace': 'mr/core',
  138. 'defs': {},
  139. },
  140.  
  141. 'SourceCodeVersion_gen': {
  142. 'target.rule': 'genrule',
  143. 'name': 'SourceCodeVersion_gen',
  144. 'out': 'SourceCodeVersion.cpp',
  145. 'cmd': 'echo "#include <SourceCodeVersion/SourceCodeVersion.h>" > $OUT && '
  146. 'echo "const char* sourcecodeversion = \\\\"VERSIONHASH\\\\";" >> $OUT && '
  147. 'echo "const char* sourcecodebranch = \\\\"BRANCHNAME\\\\";" >> $OUT',
  148. },
  149. }
  150.  
  151. IGNORED = {
  152. 'TracerExample-disabled',
  153. 'bolt_comet_interface-disabled',
  154. 'comet_app_patchsearch',
  155. 'comet_app_patchwarp',
  156. 'neon_app_pyramid',
  157. 'comet_app_pyramid',
  158. 'comet_app_corner',
  159. # complicated moc
  160. 'TrackLab',
  161. 'TrackLabOld',
  162. # doesn't work with ninja as well
  163. 'asterix_visual_debugger',
  164. # different versions of gtest
  165. 'unit_test_april_tag_detector',
  166. 'unit_test_viper_exposure_control_base',
  167. }
  168.  
  169.  
  170. def generate_target(cmake_target, mapping, out):
  171. name = cmake_target['name']
  172.  
  173. if name == 'SourceCodeVersion':
  174. buck_write.print_json_target(out, OVERRIDEN['SourceCodeVersion_gen'])
  175.  
  176. if name in OVERRIDEN:
  177. buck_write.print_json_target(out, OVERRIDEN[name])
  178. return
  179.  
  180. if name in IGNORED:
  181. return
  182.  
  183. source_directory = cmake_target['sourceDirectory']
  184. rel_dir = os.path.relpath(source_directory, start=SRC_DIR)
  185.  
  186. file_groups = cmake_target.get('fileGroups', [])
  187.  
  188. sources = []
  189. headers = []
  190. local_sources = []
  191. local_headers = []
  192. compiler_flags = []
  193. includes = []
  194. system_includes = []
  195. defines = []
  196.  
  197. is_test = False
  198.  
  199. # TODO - add headers here
  200. for file_group in file_groups:
  201. if 'language' in file_group:
  202. language = file_group['language']
  203. if language == 'CXX':
  204. if 'sources' in file_group:
  205. if len(sources) > 0:
  206. raise Exception('sources! {}'.format(name))
  207. local_sources.extend(file_group['sources'])
  208. if 'compileFlags' in file_group:
  209. compiler_flags = file_group['compileFlags'].split()
  210. if 'includePath' in file_group:
  211. for i in file_group['includePath']:
  212. p = i['path']
  213. if 'googletest' in p:
  214. is_test = True
  215. continue
  216.  
  217. if p.startswith('/Users/ilyaklyuchnikov'):
  218. p = os.path.relpath(p, start=SRC_DIR)
  219. includes.append(p)
  220. if "isSystem" in i:
  221. system_includes.append(p)
  222. if 'defines' in file_group:
  223. defines = file_group['defines']
  224. elif file_group['isGenerated'] is False:
  225. local_headers.extend(file_group['sources'])
  226.  
  227. for local_src in local_sources:
  228. if local_src.endswith('SourceCodeVersion.cpp'):
  229. continue
  230. if local_src.endswith('mocs_compilation.cpp'):
  231. continue
  232.  
  233. if local_src.startswith('../'):
  234. # print('trying normalize {}'.format(local_src))
  235. abs_path = os.path.normpath(os.path.join(source_directory, local_src))
  236. if os.path.isfile(abs_path):
  237. # print('normalized to {}'.format(abs_path))
  238. src = os.path.relpath(abs_path, start=SRC_DIR)
  239. sources.append(src)
  240. else:
  241. print('skipping {}'.format(local_src))
  242. continue
  243.  
  244. src = os.path.join(rel_dir, local_src)
  245. sources.append(src)
  246.  
  247. for local_hdr in local_headers:
  248.  
  249. if local_hdr.startswith('../'):
  250. print('skipping {}'.format(local_hdr))
  251. continue
  252.  
  253. hdr = os.path.join(rel_dir, local_hdr)
  254. headers.append(hdr)
  255.  
  256. deps = [':BuildFlags']
  257. linker_flags = []
  258.  
  259. if 'linkLibraries' in cmake_target:
  260. _linker_flags = cmake_target['linkLibraries'].split()
  261.  
  262. for lg in _linker_flags:
  263. if 'googletest' in lg:
  264. continue
  265.  
  266. lg = lg.replace(SRC_DIR, '')
  267.  
  268. if lg == '-lviper_test':
  269. continue
  270.  
  271. if lg.startswith('-'):
  272. linker_flags.append(lg)
  273. continue
  274.  
  275. i = lg.find("Software/CoreTech")
  276. if i != -1:
  277. lg = lg[i:]
  278. linker_flags.append(lg)
  279. continue
  280.  
  281. i = lg.find("third-party/qt/")
  282. if i != -1:
  283. lg = lg[i:]
  284. linker_flags.append(lg)
  285. continue
  286.  
  287. i = lg.find("third-party/realsense/")
  288. if i != -1:
  289. lg = lg[i:]
  290. linker_flags.append(lg)
  291. continue
  292.  
  293. i = lg.rfind('/')
  294. if i != -1 and lg.endswith('.a'):
  295. lib = lg[i + 1:]
  296. if lib in mapping:
  297. dep = mapping[lib]
  298. deps.append(':{}'.format(dep))
  299. continue
  300.  
  301. i = lg.rfind('/')
  302. if i != -1 and lg.endswith('.dylib'):
  303. lib = lg[i + 1:]
  304. if lib in mapping:
  305. dep = mapping[lib]
  306. deps.append(':{}'.format(dep))
  307. continue
  308.  
  309. linker_flags.append(lg)
  310.  
  311. if is_test:
  312. deps.append('//third-party/googletest:gtest')
  313.  
  314. if name == 'MontereyRecorder':
  315. linker_flags.append('Software/CoreTech/vision-third-party-libs/lz4/1.8.1.2/libs/x64-macos/liblz4.a')
  316.  
  317. preprocessor_flags = []
  318.  
  319. for define in defines:
  320. if define.startswith('QT_TESTCASE_BUILDDIR'):
  321. continue
  322. preprocessor_flags.append('-D{}'.format(define))
  323.  
  324. include_qt = False
  325.  
  326. for include in includes:
  327. if include.startswith('third-party/qt/qt_5.10.0/macos-x64/lib'):
  328. include_qt = True
  329. if include.endswith('.framework'):
  330. continue
  331.  
  332. if include == 'viper/viper/core/viper_core_BuiltInConfigs':
  333. if name != 'viper_core_BuiltInConfigs':
  334. deps.append(':viper_core_BuiltInConfigs')
  335. continue
  336.  
  337. if include == 'viper/viper/core/viper_core_CompileDefinitions':
  338. deps.append(':viper_core_CompileDefinitions')
  339. continue
  340.  
  341. if include == 'cmake/TestConfig/TestConfig':
  342. deps.append(':TestConfig')
  343. continue
  344.  
  345. if include == 'TrackingComponents/Libs/Visualization/ResourcesInterface':
  346. deps.append(':ResourcesInterface')
  347. continue
  348.  
  349. if include == 'src/Libs/Telemetry/TelemetryConfig':
  350. deps.append(':TelemetryConfig')
  351. continue
  352.  
  353. if include == 'optimizer/OptimizerCompileDefinitions':
  354. deps.append(':OptimizerCompileDefinitions')
  355. continue
  356.  
  357. if include == 'mixedreality':
  358. deps.append(':MixedrealityCompileDefinitions')
  359. continue
  360.  
  361. if include == 'TrackingComponents/Engines/ConstellationTrackingConfig':
  362. deps.append(':ConstellationTrackingConfig')
  363. continue
  364.  
  365. if include == 'TrackingComponents/Management/Recording/RecordingCompileDefinitions':
  366. deps.append(':RecordingCompileDefinitions')
  367. continue
  368.  
  369. if include == 'bolt/include':
  370. deps.append(':BoltConfig')
  371. continue
  372.  
  373. if include == 'TrackingComponents/Loaders/Athena/AthenaVersion':
  374. deps.append(':AthenaVersion')
  375. continue
  376.  
  377. if include == 'TrackingComponents/Loaders/Athena2/Athena2Version':
  378. deps.append(':Athena2Version')
  379. continue
  380.  
  381. if include in system_includes:
  382. preprocessor_flags.append('-isystem')
  383. preprocessor_flags.append(include)
  384. else:
  385. preprocessor_flags.append('-I')
  386. preprocessor_flags.append(include)
  387.  
  388. if include_qt:
  389. preprocessor_flags.append('-iframework')
  390. preprocessor_flags.append('third-party/qt/qt_5.10.0/macos-x64/lib')
  391.  
  392. if name == 'SourceCodeVersion':
  393. sources.append(':SourceCodeVersion_gen')
  394.  
  395. # if name == 'bolt_lib':
  396. # print(BOLT_CONFIG, file=out)
  397. # deps.append(':BoltConfig')
  398.  
  399. if name == 'VRSplayer':
  400. sources.append(':moc_FramePlayer')
  401. sources.append(':moc_FrameWidget')
  402. sources.append(':moc_PlayerUI')
  403. sources.append(':moc_VideoPlayer')
  404. preprocessor_flags.append('-ISoftware/CoreTech/src/Loaders/VRSplayer')
  405.  
  406. if name == 'Athena':
  407. sources.append(':moc_Athena_fileutilities')
  408. sources.append(':moc_Athena_DataTreeInterface')
  409. sources.append(':moc_Athena_glbaseview')
  410. sources.append(':moc_Athena_visionlibinterface')
  411. preprocessor_flags.append('-ISoftware/CoreTech/src/Loaders/Athena')
  412.  
  413. if name == 'Athena2':
  414. sources.append(':moc_Athena2_fileutilities')
  415. sources.append(':moc_Athena2_DataTreeInterface')
  416. sources.append(':moc_Athena2_glbaseview')
  417. sources.append(':moc_Athena2_ApolloAdapter')
  418. preprocessor_flags.append('-ISoftware/CoreTech/src/Loaders/Athena2')
  419.  
  420. if name == 'Graphing':
  421. sources.append(':moc_Graphing_FPSDisplay')
  422. sources.append(':moc_Graphing_QtGraphRenderer')
  423. sources.append(':moc_Graphing_qcustomplot')
  424. preprocessor_flags.append('-ISoftware/CoreTech/src/Libs/Graphing')
  425.  
  426. buck_target = {}
  427. buck_target['name'] = cmake_target['name']
  428. if len(sources) > 0:
  429. buck_target['srcs'] = sources
  430.  
  431. if len(headers) > 0:
  432. buck_target['raw_headers'] = headers
  433.  
  434. buck_target['preprocessor_flags'] = preprocessor_flags
  435. buck_target['compiler_flags'] = compiler_flags
  436. buck_target['deps'] = deps
  437. if len(linker_flags) > 0:
  438. buck_target['linker_flags'] = linker_flags
  439.  
  440. if cmake_target['type'] == 'SHARED_LIBRARY':
  441. buck_target['link_style'] = 'static'
  442. buck_target['preferred_linkage'] = 'shared'
  443. SHARED_TGS.append(name)
  444. # pass
  445.  
  446. if name == 'viper_core_BuiltInConfigs':
  447. buck_target['header_namespace'] = 'viper/core'
  448. buck_target['cpp_filename'] = 'BuiltInConfigs.cpp'
  449. buck_target['header_filename'] = 'BuiltInConfigs.h'
  450. buck_target['class_name'] = 'BuiltInConfigs'
  451. buck_target['namespace'] = 'viper'
  452. buck_target['config_files'] = buck_write.Glob('Software/CoreTech/viper/*.conf')
  453. buck_write.print_add_builtin_configs(out, **buck_target)
  454. return
  455.  
  456. if name == 'TestConfigs':
  457. buck_target['header_namespace'] = ''
  458. buck_target['cpp_filename'] = 'TestConfigs.cpp'
  459. buck_target['header_filename'] = 'TestConfigs.h'
  460. buck_target['class_name'] = 'TestConfigs'
  461. buck_target['namespace'] = 'coretech::config'
  462. buck_target['config_files'] = [
  463. 'Software/CoreTech/src/Libs/Config/test/configs/default/test.conf',
  464. 'Software/CoreTech/src/Libs/Config/test/configs/default/additional.conf',
  465. ]
  466. buck_write.print_add_builtin_configs(out, **buck_target)
  467. return
  468.  
  469. if name == 'DuplicateConfigs':
  470. buck_target['header_namespace'] = ''
  471. buck_target['cpp_filename'] = 'DuplicateConfigs.cpp'
  472. buck_target['header_filename'] = 'DuplicateConfigs.h'
  473. buck_target['class_name'] = 'DuplicateConfigs'
  474. buck_target['namespace'] = 'coretech::config'
  475. buck_target['config_files'] = [
  476. 'Software/CoreTech/src/Libs/Config/test/configs/duplicate/test.conf',
  477. ]
  478. buck_write.print_add_builtin_configs(out, **buck_target)
  479. return
  480.  
  481. if name == 'IncompatibleSizeConfigs':
  482. buck_target['header_namespace'] = ''
  483. buck_target['cpp_filename'] = 'IncompatibleSizeConfigs.cpp'
  484. buck_target['header_filename'] = 'IncompatibleSizeConfigs.h'
  485. buck_target['class_name'] = 'IncompatibleSizeConfigs'
  486. buck_target['namespace'] = 'coretech::config'
  487. buck_target['config_files'] = [
  488. 'Software/CoreTech/src/Libs/Config/test/configs/incompatible_size/test.conf',
  489. ]
  490. buck_write.print_add_builtin_configs(out, **buck_target)
  491. return
  492.  
  493. if name == 'IncompatibleElementConfigs':
  494. buck_target['header_namespace'] = ''
  495. buck_target['cpp_filename'] = 'IncompatibleElementConfigs.cpp'
  496. buck_target['header_filename'] = 'IncompatibleElementConfigs.h'
  497. buck_target['class_name'] = 'IncompatibleElementConfigs'
  498. buck_target['namespace'] = 'coretech::config'
  499. buck_target['config_files'] = [
  500. 'Software/CoreTech/src/Libs/Config/test/configs/incompatible_element/test.conf',
  501. ]
  502. buck_write.print_add_builtin_configs(out, **buck_target)
  503. return
  504.  
  505. if cmake_target['type'] == 'EXECUTABLE':
  506. buck_write.print_cxx_binary(out, **buck_target)
  507. else:
  508. buck_write.print_cxx_library(out, **buck_target)
  509.  
  510.  
  511. def generate_targets(cmake_targets, buck_out):
  512.  
  513. mapping = {}
  514. for cmake_target in cmake_targets:
  515. type = cmake_target['type']
  516. if type == 'STATIC_LIBRARY':
  517. mapping[cmake_target['fullName']] = cmake_target['name']
  518. if type == 'SHARED_LIBRARY':
  519. mapping[cmake_target['fullName']] = cmake_target['name']
  520.  
  521. print(START, file=buck_out)
  522. print(moc_rules.MOC_RULES, file=buck_out)
  523. buck_write.print_json_target(buck_out, OVERRIDEN['BoltConfig'])
  524. buck_write.print_json_target(buck_out, OVERRIDEN['MixedrealityCompileDefinitions'])
  525.  
  526. for cmake_target in cmake_targets:
  527. generate_target(cmake_target, mapping, buck_out)
  528.  
  529.  
  530. def generate1(input):
  531. with open(input, 'r') as infile:
  532. codemodel_json = json.load(infile)
  533.  
  534. codemodel = codemodel_json[0]
  535. generate(codemodel)
  536.  
  537.  
  538. def generate(cmake_code_model):
  539. projects = cmake_code_model['configurations'][0]['projects']
  540.  
  541. for project in projects:
  542. if project['name'] == 'CoreTech':
  543. top_project = project
  544.  
  545. targets = top_project['targets']
  546.  
  547. targets.sort(key=lambda r: r['name'])
  548.  
  549. # with open('/Users/ilyaklyuchnikov/ovrsource/BUCK_CORETECH_MAC_10', 'w') as buck_out:
  550. with open('BUCK_CORETECH_MAC1', 'w') as buck_out:
  551. generate_targets(targets, buck_out)
  552.  
  553. stgs = ''
  554. for tg in SHARED_TGS:
  555. stgs = stgs + '//:{}#default,shared '.format(tg)
  556.  
  557. print(stgs)
  558.  
  559.  
  560. def main():
  561. # import sys
  562. # print(sys.argv)
  563. with open('export/01/codemodel.json') as f:
  564. generate(json.load(f)[0])
  565.  
  566.  
  567. if __name__ == '__main__':
  568. main()
Add Comment
Please, Sign In to add comment