rules.gni 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. # Copyright (c) 2020 Project CHIP Authors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import("//build_overrides/chip.gni")
  15. import("${chip_root}/build/chip/java/config.gni")
  16. import("${chip_root}/build/config/android/config.gni")
  17. javac_runner = "${chip_root}/build/chip/java/javac_runner.py"
  18. kotlinc_runner = "${chip_root}/build/chip/java/kotlinc_runner.py"
  19. jar_runner = "${chip_root}/build/chip/java/jar_runner.py"
  20. write_build_config = "${chip_root}/build/chip/java/write_build_config.py"
  21. assert(android_sdk_root != "" || matter_enable_java_compilation,
  22. "android_sdk_root must be specified or JAVA_PATH must be set.")
  23. # Declare a java library target
  24. #
  25. # sources: List of .java files included in this library. Mutually exclusive with jar_path.
  26. #
  27. # jar_path: A path to an existing JAR. Mutually exclusive with sources.
  28. #
  29. # deps: List of dependent .jar files needed by this library.
  30. #
  31. # output_name: File name for the output .jar (not including extension).
  32. # Defaults to the input .jar file name.
  33. #
  34. # javac_flags: additional flags to pass to the javac compiler
  35. #
  36. template("java_library") {
  37. # Figure out the output name
  38. _jar_name = target_name
  39. if (defined(invoker.output_name)) {
  40. _jar_name = invoker.output_name
  41. } else {
  42. _jar_name += ".jar"
  43. }
  44. _deps = []
  45. if (defined(invoker.deps)) {
  46. _deps = invoker.deps
  47. }
  48. # What files will be compiled
  49. _java_files = []
  50. if (defined(invoker.sources)) {
  51. _java_files = invoker.sources
  52. }
  53. _is_prebuilt = defined(invoker.jar_path)
  54. _jar_output = ""
  55. _target_dir_name = get_label_info(":$target_name", "dir")
  56. if (_is_prebuilt) {
  57. assert(_java_files == [])
  58. _jar_output = "$root_out_dir/lib/$_target_dir_name/" +
  59. get_path_info(invoker.jar_path, "name") + ".jar"
  60. } else {
  61. _jar_output = "$root_out_dir/lib/$_target_dir_name/$_jar_name"
  62. }
  63. # Generate a list containing the expected build_config filepath of every dependency.
  64. _deps_configs = []
  65. foreach(_dep, _deps) {
  66. _dep_gen_dir = get_label_info(_dep, "target_gen_dir")
  67. _dep_name = get_label_info(_dep, "name")
  68. _dep_config = "$_dep_gen_dir/$_dep_name.json"
  69. _deps_configs += [ _dep_config ]
  70. }
  71. _rebased_deps_configs = rebase_path(_deps_configs, root_build_dir)
  72. # Create the name for this target's build_config.
  73. _library_target_name = target_name
  74. _build_config = "$target_gen_dir/$_library_target_name.json"
  75. _rebased_build_config = rebase_path(_build_config, root_build_dir)
  76. # Write the build_config file for this target.
  77. _config_target_name = target_name + "_config"
  78. action(_config_target_name) {
  79. script = write_build_config
  80. deps = _deps
  81. outputs = [ _build_config ]
  82. args = [
  83. "--jar-path",
  84. rebase_path(_jar_output, root_build_dir),
  85. "--build-config",
  86. _rebased_build_config,
  87. "--deps-configs=$_rebased_deps_configs",
  88. ]
  89. }
  90. # Building from sources - perform Java compilation and JAR creation.
  91. if (!_is_prebuilt) {
  92. # Additional flags
  93. _javac_flags = [
  94. "-Werror",
  95. "-Xlint:all",
  96. ]
  97. if (defined(invoker.javac_flags)) {
  98. _javac_flags += invoker.javac_flags
  99. }
  100. # Data deps
  101. _data_deps = []
  102. if (defined(invoker.data_deps)) {
  103. _data_deps = invoker.data_deps
  104. }
  105. # Generates a .sources file containing all sources to be compiled
  106. _java_sources_file = "$target_gen_dir/$target_name.sources"
  107. if (defined(invoker.java_sources_file)) {
  108. _java_sources_file = invoker.java_sources_file
  109. }
  110. write_file(_java_sources_file, rebase_path(_java_files, root_build_dir))
  111. # Compiles the given files into a directory and generates a 'class list'
  112. _javac_target_name = target_name + "__javac"
  113. _class_dir = rebase_path(target_out_dir, root_build_dir) + "/" +
  114. target_name + "/classes"
  115. _class_list_file = "$target_gen_dir/$target_name.classlist"
  116. action(_javac_target_name) {
  117. sources = _java_files
  118. deps = [ ":$_config_target_name" ]
  119. outputs = [ _class_list_file ]
  120. script = javac_runner
  121. args = [
  122. "--classdir",
  123. _class_dir,
  124. "--outfile",
  125. rebase_path(_class_list_file, root_build_dir),
  126. "--build-config",
  127. _rebased_build_config,
  128. "--",
  129. "-d",
  130. _class_dir,
  131. "@" + rebase_path(_java_sources_file, root_build_dir),
  132. ] + _javac_flags
  133. }
  134. # Bundles all files within the 'class directory' into a jar file
  135. action(target_name) {
  136. deps = [ ":$_javac_target_name" ] + _deps
  137. data_deps = _data_deps
  138. outputs = [ _jar_output ]
  139. script = jar_runner
  140. args = [
  141. "cf",
  142. rebase_path(_jar_output, root_build_dir),
  143. "-C",
  144. _class_dir,
  145. ".",
  146. ]
  147. }
  148. } else {
  149. # Using pre-specified JAR instead of building from sources - simply copy the JAR to the output directory.
  150. _original_jar_path = invoker.jar_path
  151. copy(target_name) {
  152. sources = [ _original_jar_path ]
  153. outputs = [ _jar_output ]
  154. deps = [ ":$_config_target_name" ] + _deps
  155. }
  156. }
  157. }
  158. # Declare a Java executable target
  159. #
  160. # Manifext.txt: contains the given class as the entrypoint about the files packaged in a Java executable target.
  161. #
  162. # sources: List of .java files included in this binary. Mutually exclusive with jar_path.
  163. #
  164. # jar_path: A path to an existing JAR. Mutually exclusive with sources.
  165. #
  166. # deps: List of dependent .jar files needed by this binary.
  167. #
  168. # output_name: File name for the output binary under root_build_dir/bin.
  169. #
  170. # javac_flags: additional flags to pass to the javac compiler
  171. #
  172. template("java_binary") {
  173. # Figure out the output name
  174. _jar_name = target_name
  175. if (defined(invoker.output_name)) {
  176. _jar_name = invoker.output_name
  177. } else {
  178. _jar_name += ".jar"
  179. }
  180. _deps = []
  181. if (defined(invoker.deps)) {
  182. _deps = invoker.deps
  183. }
  184. # What files will be compiled
  185. _java_files = []
  186. if (defined(invoker.sources)) {
  187. _java_files = invoker.sources
  188. }
  189. _is_prebuilt = defined(invoker.jar_path)
  190. _jar_output = ""
  191. _target_dir_name = get_label_info(":$target_name", "dir")
  192. if (_is_prebuilt) {
  193. assert(_java_files == [])
  194. _jar_output = "$root_out_dir/bin/$_target_dir_name/" +
  195. get_path_info(invoker.jar_path, "name") + ".jar"
  196. } else {
  197. _jar_output = "$root_out_dir/bin/$_target_dir_name/$_jar_name"
  198. }
  199. # Generate a list containing the expected build_config filepath of every dependency.
  200. _deps_configs = []
  201. foreach(_dep, _deps) {
  202. _dep_gen_dir = get_label_info(_dep, "target_gen_dir")
  203. _dep_name = get_label_info(_dep, "name")
  204. _dep_config = "$_dep_gen_dir/$_dep_name.json"
  205. _deps_configs += [ _dep_config ]
  206. }
  207. _rebased_deps_configs = rebase_path(_deps_configs, root_build_dir)
  208. # Create the name for this target's build_config.
  209. _library_target_name = target_name
  210. _build_config = "$target_gen_dir/$_library_target_name.json"
  211. _rebased_build_config = rebase_path(_build_config, root_build_dir)
  212. # Write the build_config file for this target.
  213. _config_target_name = target_name + "_config"
  214. action(_config_target_name) {
  215. script = write_build_config
  216. deps = _deps
  217. outputs = [ _build_config ]
  218. args = [
  219. "--jar-path",
  220. rebase_path(_jar_output, root_build_dir),
  221. "--build-config",
  222. _rebased_build_config,
  223. "--deps-configs=$_rebased_deps_configs",
  224. ]
  225. }
  226. # Building from sources - perform Java compilation and JAR creation.
  227. if (!_is_prebuilt) {
  228. # Additional flags
  229. _javac_flags = [
  230. "-Werror",
  231. "-Xlint:all",
  232. ]
  233. if (defined(invoker.javac_flags)) {
  234. _javac_flags += invoker.javac_flags
  235. }
  236. # Data deps
  237. _data_deps = []
  238. if (defined(invoker.data_deps)) {
  239. _data_deps = invoker.data_deps
  240. }
  241. # Generates a .sources file containing all sources to be compiled
  242. _java_sources_file = "$target_gen_dir/$target_name.sources"
  243. if (defined(invoker.java_sources_file)) {
  244. _java_sources_file = invoker.java_sources_file
  245. }
  246. write_file(_java_sources_file, rebase_path(_java_files, root_build_dir))
  247. # Compiles the given files into a directory and generates a 'class list'
  248. _javac_target_name = target_name + "__javac"
  249. _class_dir = rebase_path(target_out_dir, root_build_dir) + "/" +
  250. target_name + "/classes"
  251. _class_list_file = "$target_gen_dir/$target_name.classlist"
  252. action(_javac_target_name) {
  253. sources = _java_files
  254. deps = [ ":$_config_target_name" ]
  255. outputs = [ _class_list_file ]
  256. script = javac_runner
  257. args = [
  258. "--classdir",
  259. _class_dir,
  260. "--outfile",
  261. rebase_path(_class_list_file, root_build_dir),
  262. "--build-config",
  263. _rebased_build_config,
  264. "--",
  265. "-d",
  266. _class_dir,
  267. "@" + rebase_path(_java_sources_file, root_build_dir),
  268. ] + _javac_flags
  269. }
  270. # Bundles all files within the 'class directory' into a jar file
  271. action(target_name) {
  272. deps = [ ":$_javac_target_name" ] + _deps
  273. data_deps = _data_deps
  274. outputs = [ _jar_output ]
  275. script = jar_runner
  276. args = [
  277. "cfm",
  278. rebase_path(_jar_output, root_build_dir),
  279. "Manifest.txt",
  280. "-C",
  281. _class_dir,
  282. ".",
  283. ]
  284. }
  285. } else {
  286. # Using pre-specified JAR instead of building from sources - simply copy the JAR to the output directory.
  287. _original_jar_path = invoker.jar_path
  288. copy(target_name) {
  289. sources = [ _original_jar_path ]
  290. outputs = [ _jar_output ]
  291. deps = [ ":$_config_target_name" ] + _deps
  292. }
  293. }
  294. }
  295. # Declare a kotlin library target
  296. #
  297. # sources: List of .kt files included in this library. Mutually exclusive with jar_path.
  298. #
  299. # jar_path: A path to an existing JAR. Mutually exclusive with sources.
  300. #
  301. # deps: List of dependent .jar files needed by this library.
  302. #
  303. # output_name: File name for the output .jar (not including extension).
  304. # Defaults to the input .jar file name.
  305. #
  306. # kotlinc_flags: additional flags to pass to the kotlinc compiler
  307. #
  308. template("kotlin_library") {
  309. # Figure out the output name
  310. _jar_name = target_name
  311. if (defined(invoker.output_name)) {
  312. _jar_name = invoker.output_name
  313. } else {
  314. _jar_name += ".jar"
  315. }
  316. _deps = []
  317. if (defined(invoker.deps)) {
  318. _deps = invoker.deps
  319. }
  320. # What files will be compiled
  321. _kotlin_files = []
  322. if (defined(invoker.sources)) {
  323. _kotlin_files = invoker.sources
  324. }
  325. _is_prebuilt = defined(invoker.jar_path)
  326. _jar_output = ""
  327. _target_dir_name = get_label_info(":$target_name", "dir")
  328. if (_is_prebuilt) {
  329. assert(_kotlin_files == [])
  330. _jar_output = "$root_out_dir/lib/$_target_dir_name/" +
  331. get_path_info(invoker.jar_path, "name") + ".jar"
  332. } else {
  333. _jar_output = "$root_out_dir/lib/$_target_dir_name/$_jar_name"
  334. }
  335. # Generate a list containing the expected build_config filepath of every dependency.
  336. _deps_configs = []
  337. foreach(_dep, _deps) {
  338. _dep_gen_dir = get_label_info(_dep, "target_gen_dir")
  339. _dep_name = get_label_info(_dep, "name")
  340. _dep_config = "$_dep_gen_dir/$_dep_name.json"
  341. _deps_configs += [ _dep_config ]
  342. }
  343. _rebased_deps_configs = rebase_path(_deps_configs, root_build_dir)
  344. # Create the name for this target's build_config.
  345. _library_target_name = target_name
  346. _build_config = "$target_gen_dir/$_library_target_name.json"
  347. _rebased_build_config = rebase_path(_build_config, root_build_dir)
  348. # Write the build_config file for this target.
  349. _config_target_name = target_name + "_config"
  350. action(_config_target_name) {
  351. script = write_build_config
  352. deps = _deps
  353. outputs = [ _build_config ]
  354. args = [
  355. "--jar-path",
  356. rebase_path(_jar_output, root_build_dir),
  357. "--build-config",
  358. _rebased_build_config,
  359. "--deps-configs=$_rebased_deps_configs",
  360. ]
  361. }
  362. # Building from sources - perform Kotlin compilation and JAR creation.
  363. if (!_is_prebuilt) {
  364. # Additional flags
  365. _kotlinc_flags = [
  366. "-Werror",
  367. "-Xlint:all",
  368. ]
  369. if (defined(invoker.kotlinc_flags)) {
  370. _kotlinc_flags += invoker.kotlinc_flags
  371. }
  372. # Data deps
  373. _data_deps = []
  374. if (defined(invoker.data_deps)) {
  375. _data_deps = invoker.data_deps
  376. }
  377. # Generates a .sources file containing all sources to be compiled
  378. _kotlin_sources_file = "$target_gen_dir/$target_name.sources"
  379. if (defined(invoker.kotlin_sources_file)) {
  380. _kotlin_sources_file = invoker.kotlin_sources_file
  381. }
  382. write_file(_kotlin_sources_file, rebase_path(_kotlin_files, root_build_dir))
  383. # Compiles the given files into a directory and generates a 'class list'
  384. _kotlinc_target_name = target_name + "__kotlinc"
  385. _class_dir = rebase_path(target_out_dir, root_build_dir) + "/" +
  386. target_name + "/classes"
  387. _class_list_file = "$target_gen_dir/$target_name.classlist"
  388. action(_kotlinc_target_name) {
  389. sources = _kotlin_files
  390. deps = [ ":$_config_target_name" ]
  391. outputs = [ _class_list_file ]
  392. script = kotlinc_runner
  393. args = [
  394. "--classdir",
  395. _class_dir,
  396. "--outfile",
  397. rebase_path(_class_list_file, root_build_dir),
  398. "--build-config",
  399. _rebased_build_config,
  400. "--",
  401. "-d",
  402. _class_dir,
  403. "@" + rebase_path(_kotlin_sources_file, root_build_dir),
  404. ] + _kotlinc_flags
  405. }
  406. # Bundles all files within the 'class directory' into a jar file
  407. action(target_name) {
  408. deps = [ ":$_kotlinc_target_name" ] + _deps
  409. data_deps = _data_deps
  410. outputs = [ _jar_output ]
  411. script = jar_runner
  412. args = [
  413. "cf",
  414. rebase_path(_jar_output, root_build_dir),
  415. "-C",
  416. _class_dir,
  417. ".",
  418. ]
  419. }
  420. } else {
  421. # Using pre-specified JAR instead of building from sources - simply copy the JAR to the output directory.
  422. _original_jar_path = invoker.jar_path
  423. copy(target_name) {
  424. sources = [ _original_jar_path ]
  425. outputs = [ _jar_output ]
  426. deps = [ ":$_config_target_name" ] + _deps
  427. }
  428. }
  429. }
  430. # Declare a Kotlin executable target
  431. #
  432. # Manifext.txt: contains the given class as the entrypoint about the files packaged in a Kotlin executable target.
  433. #
  434. # sources: List of .kt files included in this binary. Mutually exclusive with jar_path.
  435. #
  436. # jar_path: A path to an existing JAR. Mutually exclusive with sources.
  437. #
  438. # deps: List of dependent .jar files needed by this binary.
  439. #
  440. # output_name: File name for the output binary under root_build_dir/bin.
  441. #
  442. # kotlinc_flags: additional flags to pass to the kotlinc compiler
  443. #
  444. template("kotlin_binary") {
  445. # Figure out the output name
  446. _jar_name = target_name
  447. if (defined(invoker.output_name)) {
  448. _jar_name = invoker.output_name
  449. } else {
  450. _jar_name += ".jar"
  451. }
  452. _deps = []
  453. if (defined(invoker.deps)) {
  454. _deps = invoker.deps
  455. }
  456. # What files will be compiled
  457. _kotlin_files = []
  458. if (defined(invoker.sources)) {
  459. _kotlin_files = invoker.sources
  460. }
  461. _is_prebuilt = defined(invoker.jar_path)
  462. _jar_output = ""
  463. _target_dir_name = get_label_info(":$target_name", "dir")
  464. if (_is_prebuilt) {
  465. assert(_kotlin_files == [])
  466. _jar_output = "$root_out_dir/bin/$_target_dir_name/" +
  467. get_path_info(invoker.jar_path, "name") + ".jar"
  468. } else {
  469. _jar_output = "$root_out_dir/bin/$_target_dir_name/$_jar_name"
  470. }
  471. # Generate a list containing the expected build_config filepath of every dependency.
  472. _deps_configs = []
  473. foreach(_dep, _deps) {
  474. _dep_gen_dir = get_label_info(_dep, "target_gen_dir")
  475. _dep_name = get_label_info(_dep, "name")
  476. _dep_config = "$_dep_gen_dir/$_dep_name.json"
  477. _deps_configs += [ _dep_config ]
  478. }
  479. _rebased_deps_configs = rebase_path(_deps_configs, root_build_dir)
  480. # Create the name for this target's build_config.
  481. _library_target_name = target_name
  482. _build_config = "$target_gen_dir/$_library_target_name.json"
  483. _rebased_build_config = rebase_path(_build_config, root_build_dir)
  484. # Write the build_config file for this target.
  485. _config_target_name = target_name + "_config"
  486. action(_config_target_name) {
  487. script = write_build_config
  488. deps = _deps
  489. outputs = [ _build_config ]
  490. args = [
  491. "--jar-path",
  492. rebase_path(_jar_output, root_build_dir),
  493. "--build-config",
  494. _rebased_build_config,
  495. "--deps-configs=$_rebased_deps_configs",
  496. ]
  497. }
  498. # Building from sources - perform Kotlin compilation and JAR creation.
  499. if (!_is_prebuilt) {
  500. # Additional flags
  501. _kotlinc_flags = [
  502. "-Werror",
  503. "-Xlint:all",
  504. ]
  505. if (defined(invoker.kotlinc_flags)) {
  506. _kotlinc_flags += invoker.kotlinc_flags
  507. }
  508. # Data deps
  509. _data_deps = []
  510. if (defined(invoker.data_deps)) {
  511. _data_deps = invoker.data_deps
  512. }
  513. # Generates a .sources file containing all sources to be compiled
  514. _kotlin_sources_file = "$target_gen_dir/$target_name.sources"
  515. if (defined(invoker.kotlin_sources_file)) {
  516. _kotlin_sources_file = invoker.kotlin_sources_file
  517. }
  518. write_file(_kotlin_sources_file, rebase_path(_kotlin_files, root_build_dir))
  519. # Compiles the given files into a directory and generates a 'class list'
  520. _kotlinc_target_name = target_name + "__kotlinc"
  521. _class_dir = rebase_path(target_out_dir, root_build_dir) + "/" +
  522. target_name + "/classes"
  523. _class_list_file = "$target_gen_dir/$target_name.classlist"
  524. action(_kotlinc_target_name) {
  525. sources = _kotlin_files
  526. deps = [ ":$_config_target_name" ]
  527. outputs = [ _class_list_file ]
  528. script = kotlinc_runner
  529. args = [
  530. "--classdir",
  531. _class_dir,
  532. "--outfile",
  533. rebase_path(_class_list_file, root_build_dir),
  534. "--build-config",
  535. _rebased_build_config,
  536. "--",
  537. "-d",
  538. _class_dir,
  539. "@" + rebase_path(_kotlin_sources_file, root_build_dir),
  540. ] + _kotlinc_flags
  541. }
  542. # Bundles all files within the 'class directory' into a jar file
  543. action(target_name) {
  544. deps = [ ":$_kotlinc_target_name" ] + _deps
  545. data_deps = _data_deps
  546. outputs = [ _jar_output ]
  547. script = jar_runner
  548. args = [
  549. "cfm",
  550. rebase_path(_jar_output, root_build_dir),
  551. "Manifest.txt",
  552. "-C",
  553. _class_dir,
  554. ".",
  555. ]
  556. }
  557. } else {
  558. # Using pre-specified JAR instead of building from sources - simply copy the JAR to the output directory.
  559. _original_jar_path = invoker.jar_path
  560. copy(target_name) {
  561. sources = [ _original_jar_path ]
  562. outputs = [ _jar_output ]
  563. deps = [ ":$_config_target_name" ] + _deps
  564. }
  565. }
  566. }
  567. template("android_library") {
  568. java_library(target_name) {
  569. forward_variables_from(invoker, "*")
  570. if (!defined(javac_flags)) {
  571. javac_flags = []
  572. }
  573. javac_flags += [
  574. "-Xlint:-options",
  575. "-source",
  576. "8",
  577. "-target",
  578. "8",
  579. ]
  580. }
  581. }
  582. template("java_prebuilt") {
  583. java_library(target_name) {
  584. forward_variables_from(invoker, "*")
  585. }
  586. }