buildflag_header.gni 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # Copyright 2015 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. # Generates a header with preprocessor defines specified by the build file.
  5. #
  6. # The flags are converted to function-style defines with mangled names and
  7. # code uses an accessor macro to access the values. This is to try to
  8. # minimize bugs where code checks whether something is defined or not, and
  9. # the proper header isn't included, meaning the answer will always be silently
  10. # false or might vary across the code base.
  11. #
  12. # In the GN template, specify build flags in the template as a list
  13. # of strings that encode key/value pairs like this:
  14. #
  15. # flags = [ "ENABLE_FOO=1", "ENABLE_BAR=$enable_bar" ]
  16. #
  17. # The GN values "true" and "false" will be mapped to 0 and 1 for boolean
  18. # #if flags to be expressed naturally. This means you can't directly make a
  19. # define that generates C++ value of true or false for use in code. If you
  20. # REALLY need this, you can also use the string "(true)" and "(false)" to
  21. # prevent the rewriting.
  22. # To check the value of the flag in C code:
  23. #
  24. # #include "path/to/here/header_file.h"
  25. #
  26. # #if BUILDFLAG(ENABLE_FOO)
  27. # ...
  28. # #endif
  29. #
  30. # const char kSpamServerUrl[] = BUILDFLAG(SPAM_SERVER_URL);
  31. #
  32. # There will be no #define called ENABLE_FOO so if you accidentally test for
  33. # that in an ifdef it will always be negative.
  34. #
  35. #
  36. # Template parameters
  37. #
  38. # flags [required, list of strings]
  39. # Flag values as described above.
  40. #
  41. # header [required, string]
  42. # File name for generated header. By default, this will go in the
  43. # generated file directory for this target, and you would include it
  44. # with:
  45. # #include "<path_to_this_BUILD_file>/<header>"
  46. #
  47. # header_dir [optional, string]
  48. # Override the default location of the generated header. The string will
  49. # be treated as a subdirectory of the root_gen_dir. For example:
  50. # header_dir = "foo/bar"
  51. # Then you can include the header as:
  52. # #include "foo/bar/baz.h"
  53. #
  54. # deps, public_deps, testonly, visibility
  55. # Normal meaning.
  56. #
  57. #
  58. # Grit defines
  59. #
  60. # If one .grd file uses a flag, just add to the grit target:
  61. #
  62. # defines = [
  63. # "enable_doom_melon=$enable_doom_melon",
  64. # ]
  65. #
  66. # If multiple .grd files use it, you'll want to put the defines in a .gni file
  67. # so it can be shared. Generally this .gni file should include all grit defines
  68. # for a given module (for some definition of "module"). Then do:
  69. #
  70. # defines = ui_grit_defines
  71. #
  72. # If you forget to do this, the flag will be implicitly false in the .grd file
  73. # and those resources won't be compiled. You'll know because the resource
  74. # #define won't be generated and any code that uses it won't compile. If you
  75. # see a missing IDS_* string, this is probably the reason.
  76. #
  77. #
  78. # Example
  79. #
  80. # buildflag_header("foo_buildflags") {
  81. # header = "foo_buildflags.h"
  82. #
  83. # flags = [
  84. # # This uses the GN build flag enable_doom_melon as the definition.
  85. # "ENABLE_DOOM_MELON=$enable_doom_melon",
  86. #
  87. # # This force-enables the flag.
  88. # "ENABLE_SPACE_LASER=true",
  89. #
  90. # # This will expand to the quoted C string when used in source code.
  91. # "SPAM_SERVER_URL=\"http://www.example.com/\"",
  92. # ]
  93. # }
  94. template("buildflag_header") {
  95. action(target_name) {
  96. script = "//build/write_buildflag_header.py"
  97. if (defined(invoker.header_dir)) {
  98. header_file = "${invoker.header_dir}/${invoker.header}"
  99. } else {
  100. # Compute the path from the root to this file.
  101. header_file = rebase_path(".", "//") + "/${invoker.header}"
  102. }
  103. outputs = [ "$root_gen_dir/$header_file" ]
  104. # Always write --flags to the file so it's not empty. Empty will confuse GN
  105. # into thinking the response file isn't used.
  106. response_file_contents = [ "--flags" ]
  107. if (defined(invoker.flags)) {
  108. response_file_contents += invoker.flags
  109. }
  110. args = [
  111. "--output",
  112. header_file, # Not rebased, Python script puts it inside gen-dir.
  113. "--rulename",
  114. get_label_info(":$target_name", "label_no_toolchain"),
  115. "--gen-dir",
  116. rebase_path(root_gen_dir, root_build_dir),
  117. "--definitions",
  118. "{{response_file_name}}",
  119. ]
  120. forward_variables_from(invoker,
  121. [
  122. "deps",
  123. "public_deps",
  124. "testonly",
  125. "visibility",
  126. ])
  127. public_deps = [ "//build:buildflag_header_h" ]
  128. }
  129. }