BUILD.gn 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. # Copyright 2016 Google Inc.
  2. #
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. declare_args() {
  6. extra_asmflags = []
  7. extra_cflags = []
  8. extra_cflags_c = []
  9. extra_cflags_cc = []
  10. extra_ldflags = []
  11. malloc = ""
  12. }
  13. if (is_ios) {
  14. if (is_tvos) {
  15. sdk = "appletvos"
  16. if (target_cpu == "x86" || target_cpu == "x64") {
  17. sdk = "appletvsimulator"
  18. }
  19. } else {
  20. sdk = "iphoneos"
  21. if (target_cpu == "x86" || target_cpu == "x64") {
  22. sdk = "iphonesimulator"
  23. }
  24. }
  25. ios_sysroot = exec_script("find_ios_sysroot.py", [ sdk ], "trim string")
  26. }
  27. config("default") {
  28. asmflags = []
  29. cflags = []
  30. cflags_c = []
  31. cflags_cc = []
  32. defines = []
  33. ldflags = []
  34. libs = []
  35. if (werror) {
  36. if (is_win) {
  37. cflags += [ "/WX" ]
  38. } else {
  39. cflags += [ "-Werror" ]
  40. }
  41. }
  42. if (is_win) {
  43. if (is_clang && target_cpu == "arm64") {
  44. cflags += [ "--target=arm64-windows" ]
  45. }
  46. cflags += [
  47. "/bigobj", # Some of our files are bigger than the regular limits.
  48. "/utf-8", # Set Source and Executable character sets to UTF-8.
  49. ]
  50. if (is_clang) {
  51. cflags += [ "-fms-compatibility-version=19" ] # 2015
  52. }
  53. defines += [
  54. "_CRT_SECURE_NO_WARNINGS", # Disables warnings about sscanf().
  55. "_HAS_EXCEPTIONS=0", # Disables exceptions in MSVC STL.
  56. "WIN32_LEAN_AND_MEAN",
  57. "NOMINMAX",
  58. ]
  59. _include_dirs = [
  60. "$win_vc/Tools/MSVC/$win_toolchain_version/include",
  61. "$win_sdk/Include/$win_sdk_version/shared",
  62. "$win_sdk/Include/$win_sdk_version/ucrt",
  63. "$win_sdk/Include/$win_sdk_version/um",
  64. "$win_sdk/Include/$win_sdk_version/winrt",
  65. ]
  66. if (is_clang) {
  67. foreach(dir, _include_dirs) {
  68. cflags += [
  69. "-imsvc",
  70. dir,
  71. ]
  72. }
  73. } else {
  74. include_dirs = _include_dirs
  75. }
  76. lib_dirs = [
  77. "$win_sdk/Lib/$win_sdk_version/ucrt/$target_cpu",
  78. "$win_sdk/Lib/$win_sdk_version/um/$target_cpu",
  79. "$win_vc/Tools/MSVC/$win_toolchain_version/lib/$target_cpu",
  80. ]
  81. } else {
  82. cflags += [
  83. "-fstrict-aliasing",
  84. "-fPIC",
  85. ]
  86. cflags_cc += [ "-std=c++14" ]
  87. # The main idea is to slim the exported API, but these flags also improve link time on Mac.
  88. # These would make stack traces worse on Linux, so we don't just set them willy-nilly.
  89. if (is_component_build || is_ios || is_mac) {
  90. cflags += [ "-fvisibility=hidden" ]
  91. cflags_cc += [ "-fvisibility-inlines-hidden" ]
  92. }
  93. }
  94. if (current_cpu == "arm") {
  95. cflags += [
  96. "-march=armv7-a",
  97. "-mfpu=neon",
  98. "-mthumb",
  99. ]
  100. } else if (current_cpu == "loongson3a") {
  101. asmflags += [ "-march=loongson3a" ]
  102. cflags += [
  103. "-march=loongson3a",
  104. # Causes an internal compiler error.
  105. "-DSKCMS_PORTABLE",
  106. ]
  107. } else if (current_cpu == "mips64el") {
  108. asmflags += [ "-march=mips64" ]
  109. cflags += [ "-march=mips64" ]
  110. } else if (current_cpu == "x86" && !is_win) {
  111. asmflags += [ "-m32" ]
  112. cflags += [
  113. "-m32",
  114. "-msse2",
  115. "-mfpmath=sse",
  116. ]
  117. ldflags += [ "-m32" ]
  118. }
  119. if (malloc != "" && !is_win) {
  120. cflags += [
  121. "-fno-builtin-malloc",
  122. "-fno-builtin-calloc",
  123. "-fno-builtin-realloc",
  124. "-fno-builtin-free",
  125. ]
  126. libs += [ malloc ]
  127. }
  128. if (is_android) {
  129. cflags += [ "--sysroot=$ndk/sysroot" ]
  130. cflags_cc += [ "-isystem$ndk/sources/cxx-stl/llvm-libc++/include" ]
  131. ldflags += [ "-static-libstdc++" ]
  132. }
  133. if (is_ios) {
  134. _target = target_cpu
  135. if (target_cpu == "arm") {
  136. _target = "armv7"
  137. } else if (target_cpu == "x86") {
  138. _target = "i386"
  139. } else if (target_cpu == "x64") {
  140. _target = "x86_64"
  141. }
  142. asmflags += [
  143. "-isysroot",
  144. ios_sysroot,
  145. "-arch",
  146. _target,
  147. ]
  148. cflags += [
  149. "-isysroot",
  150. ios_sysroot,
  151. "-arch",
  152. _target,
  153. ]
  154. cflags_cc += [ "-stdlib=libc++" ]
  155. ldflags += [
  156. "-isysroot",
  157. ios_sysroot,
  158. "-arch",
  159. _target,
  160. "-stdlib=libc++",
  161. ]
  162. libs += [ "objc" ]
  163. }
  164. if (is_linux) {
  165. libs += [ "pthread" ]
  166. if (is_debug && sanitize == "") {
  167. defines += [ "_GLIBCXX_DEBUG" ]
  168. }
  169. }
  170. if (is_mac) {
  171. # Disable linker warnings. They're usually just annoyances like,
  172. # ld: warning: text-based stub file
  173. # /System/Library/Frameworks/foo.framework/foo.tbd and library file
  174. # /System/Library/Frameworks/foo.framework/foo are out of sync.
  175. # Falling back to library file for linking.
  176. ldflags += [ "-Wl,-w" ]
  177. }
  178. if (sanitize != "" && sanitize != "MSVC") {
  179. # You can either pass the sanitizers directly, e.g. "address,undefined",
  180. # or pass one of the couple common aliases used by the bots.
  181. sanitizers = sanitize
  182. if (sanitize == "ASAN" || sanitize == "UBSAN") {
  183. # ASAN implicitly runs all UBSAN checks also.
  184. sanitizers = "undefined"
  185. if (sanitize == "ASAN") {
  186. sanitizers += ",address"
  187. }
  188. if (is_android) {
  189. # TODO(mtklein): work out UBSAN link errors
  190. sanitizers = "address"
  191. }
  192. } else if (sanitize == "TSAN") {
  193. sanitizers = "thread"
  194. } else if (sanitize == "MSAN") {
  195. sanitizers = "memory"
  196. }
  197. _blacklist = rebase_path("../tools/xsan.blacklist")
  198. cflags += [
  199. "-fsanitize=$sanitizers",
  200. "-fno-sanitize-recover=$sanitizers",
  201. "-fsanitize-blacklist=$_blacklist",
  202. ]
  203. ldflags += [ "-fsanitize=$sanitizers" ]
  204. if (is_win) {
  205. cflags += [ "/FI$_blacklist" ]
  206. } else {
  207. cflags += [
  208. "-include$_blacklist",
  209. "-fno-omit-frame-pointer",
  210. ]
  211. }
  212. if (is_linux) {
  213. cflags_cc += [ "-stdlib=libc++" ]
  214. ldflags += [ "-stdlib=libc++" ]
  215. }
  216. if (sanitizers == "memory") {
  217. cflags += [ "-fsanitize-memory-track-origins" ]
  218. }
  219. }
  220. }
  221. config("no_exceptions") {
  222. # Exceptions are disabled by default on Windows. (Use /EHsc to enable them.)
  223. if (!is_win) {
  224. cflags_cc = [ "-fno-exceptions" ]
  225. }
  226. }
  227. config("warnings") {
  228. cflags = []
  229. cflags_cc = []
  230. cflags_objc = []
  231. cflags_objcc = []
  232. if (is_win) {
  233. cflags += [
  234. "/W3", # Turn on lots of warnings.
  235. # Disable a bunch of warnings:
  236. "/wd4244", # conversion from 'float' to 'int', possible loss of data
  237. "/wd4267", # conversion from 'size_t' to 'int', possible loss of data
  238. "/wd4800", # forcing value to bool 'true' or 'false' (performance warning)
  239. # Probably only triggers when /EHsc is enabled.
  240. "/wd4291", # no matching operator delete found;
  241. # memory will not be freed if initialization throws an exception
  242. # These only show up in shared builds:
  243. "/wd4251", # class 'type' needs to have dll-interface to be used by clients of class 'type2'
  244. "/wd4275", # non dll-interface class 'base' used as base for dll-interface class 'derived'
  245. ]
  246. } else {
  247. cflags += [
  248. "-Wall",
  249. "-Wextra",
  250. "-Winit-self",
  251. "-Wpointer-arith",
  252. "-Wsign-compare",
  253. "-Wvla",
  254. "-Wno-deprecated-declarations",
  255. "-Wno-maybe-uninitialized",
  256. ]
  257. cflags_cc += [
  258. "-Wnon-virtual-dtor",
  259. "-Wno-noexcept-type",
  260. ]
  261. }
  262. if (is_clang) {
  263. cflags += [
  264. "-fcolor-diagnostics",
  265. "-Weverything",
  266. "-Wno-unknown-warning-option", # Let older Clangs ignore newer Clangs' warnings.
  267. ]
  268. if (target_cpu == "arm" && is_ios) {
  269. # Clang seems to think new/malloc will only be 4-byte aligned on 32-bit iOS.
  270. # We're pretty sure it's actually 8-byte alignment.
  271. cflags += [ "-Wno-over-aligned" ]
  272. }
  273. if (target_cpu == "x86" && is_android) {
  274. # Clang seems to think new/malloc will only be 4-byte aligned on 32-bit x86 Android builds.
  275. # We're pretty sure it's actually 8-byte alignment. See OverAlignedTest.cpp for more info.
  276. cflags += [ "-Wno-over-aligned" ]
  277. }
  278. # Shouldn't be necessary for local builds. With distributed Windows builds, files may lose
  279. # their case during copy, causing case-sensitivity mismatch on remote machines.
  280. cflags += [
  281. "-Wno-nonportable-include-path",
  282. "-Wno-nonportable-system-include-path",
  283. ]
  284. # TODO: These would all be really great warnings to turn on.
  285. cflags += [
  286. "-Wno-cast-align",
  287. "-Wno-cast-qual",
  288. "-Wno-conversion",
  289. "-Wno-disabled-macro-expansion",
  290. "-Wno-documentation",
  291. "-Wno-documentation-unknown-command",
  292. "-Wno-double-promotion",
  293. "-Wno-exit-time-destructors", # TODO: OK outside libskia
  294. "-Wno-float-equal",
  295. "-Wno-format-nonliteral",
  296. "-Wno-global-constructors", # TODO: OK outside libskia
  297. "-Wno-missing-prototypes",
  298. "-Wno-missing-variable-declarations",
  299. "-Wno-pedantic",
  300. "-Wno-reserved-id-macro",
  301. "-Wno-shadow",
  302. "-Wno-shift-sign-overflow",
  303. "-Wno-signed-enum-bitfield",
  304. "-Wno-switch-enum",
  305. "-Wno-undef",
  306. "-Wno-unreachable-code",
  307. "-Wno-unreachable-code-break",
  308. "-Wno-unreachable-code-return",
  309. "-Wno-unused-macros",
  310. "-Wno-unused-member-function",
  311. "-Wno-unused-template",
  312. "-Wno-zero-as-null-pointer-constant",
  313. "-Wno-thread-safety-negative",
  314. ]
  315. cflags_cc += [
  316. "-Wno-abstract-vbase-init",
  317. "-Wno-weak-vtables",
  318. ]
  319. # We are unlikely to want to fix these.
  320. cflags += [
  321. "-Wno-bad-function-cast",
  322. "-Wno-covered-switch-default",
  323. "-Wno-deprecated",
  324. "-Wno-missing-noreturn",
  325. "-Wno-old-style-cast",
  326. "-Wno-padded",
  327. "-Wno-newline-eof",
  328. ]
  329. cflags_cc += [
  330. "-Wno-c++98-compat",
  331. "-Wno-c++98-compat-pedantic",
  332. "-Wno-undefined-func-template",
  333. ]
  334. cflags_objc += [
  335. "-Wno-direct-ivar-access",
  336. "-Wno-objc-interface-ivars",
  337. ]
  338. cflags_objcc += [
  339. "-Wno-direct-ivar-access",
  340. "-Wno-objcc-interface-ivars",
  341. ]
  342. }
  343. if (!is_win || is_clang) {
  344. cflags += [ "-Wno-implicit-fallthrough" ]
  345. }
  346. }
  347. config("warnings_except_public_headers") {
  348. if (!is_win || is_clang) {
  349. cflags = [ "-Wno-unused-parameter" ]
  350. }
  351. }
  352. config("extra_flags") {
  353. asmflags = extra_asmflags
  354. cflags = extra_cflags
  355. cflags_c = extra_cflags_c
  356. cflags_cc = extra_cflags_cc
  357. ldflags = extra_ldflags
  358. }
  359. config("debug_symbols") {
  360. # It's annoying to wait for full debug symbols to push over
  361. # to Android devices. -gline-tables-only is a lot slimmer.
  362. if (is_android) {
  363. cflags = [
  364. "-gline-tables-only",
  365. "-funwind-tables", # Helps make in-process backtraces fuller.
  366. ]
  367. } else if (is_win) {
  368. cflags = [ "/Z7" ]
  369. if (is_clang) {
  370. cflags += [ "-gcodeview-ghash" ]
  371. ldflags = [ "/DEBUG:GHASH" ]
  372. } else {
  373. ldflags = [ "/DEBUG:FASTLINK" ]
  374. }
  375. } else {
  376. cflags = [ "-g" ]
  377. }
  378. }
  379. config("no_rtti") {
  380. if (sanitize != "ASAN") { # -fsanitize=vptr requires RTTI
  381. if (is_win) {
  382. cflags_cc = [ "/GR-" ]
  383. } else {
  384. cflags_cc = [ "-fno-rtti" ]
  385. }
  386. }
  387. }
  388. config("optimize") {
  389. if (is_win) {
  390. cflags = [
  391. "/O2",
  392. "/Zc:inline",
  393. ]
  394. ldflags = [
  395. "/OPT:ICF",
  396. "/OPT:REF",
  397. ]
  398. } else {
  399. cflags = [ "-O3" ]
  400. if (is_mac || is_ios) {
  401. ldflags = [ "-dead_strip" ]
  402. } else {
  403. cflags += [
  404. "-fdata-sections",
  405. "-ffunction-sections",
  406. ]
  407. ldflags = [ "-Wl,--gc-sections" ]
  408. }
  409. if (target_cpu == "wasm") {
  410. # The compiler asks us to add an optimization flag to both cflags
  411. # and ldflags to cut down on the local variables,
  412. # for performance reasons.
  413. # The "linking" step is the conversion to javascript.
  414. ldflags += [ "-O3" ]
  415. }
  416. }
  417. }
  418. config("NDEBUG") {
  419. defines = [ "NDEBUG" ]
  420. }
  421. config("msvc_rtc") {
  422. defines = [ "_ALLOW_RTCc_IN_STL" ]
  423. cflags = [
  424. "/RTCcsu",
  425. "/guard:cf",
  426. ]
  427. ldflags = [ "/guard:cf" ]
  428. }
  429. config("executable") {
  430. if (is_android) {
  431. ldflags = [
  432. "-pie",
  433. "-rdynamic",
  434. ]
  435. } else if (is_mac) {
  436. ldflags = [ "-Wl,-rpath,@loader_path/." ]
  437. } else if (is_linux) {
  438. ldflags = [
  439. "-rdynamic",
  440. "-Wl,-rpath,\$ORIGIN",
  441. ]
  442. } else if (is_win) {
  443. ldflags = [
  444. "/SUBSYSTEM:CONSOLE", # Quiet "no subsystem specified; CONSOLE assumed".
  445. "/INCREMENTAL:NO", # Quiet warnings about failing to incrementally link by never trying to.
  446. ]
  447. }
  448. }