Skip to content

Commit ee0191a

Browse files
authored
Apply warning and error compile flags only to 1P code (#7241)
Move all the configuration of error and warning flags to after we have added the third_party subdirectory and declared all of the 3P targets. This ensures that only the basic configuration that e.g. affects ABI is applied to the 3P code. This will make it easier to add 3P code that does not compile cleanly with all the warnings and errors we enable for our own code.
1 parent ff423ae commit ee0191a

File tree

3 files changed

+109
-93
lines changed

3 files changed

+109
-93
lines changed

CMakeLists.txt

Lines changed: 108 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,26 @@ endif()
3131
# more useful error reports from users.
3232
option(BYN_ENABLE_ASSERTIONS "Enable assertions" ON)
3333

34+
option(BYN_ENABLE_LTO "Build with LTO" Off)
35+
3436
# Turn this off to avoid the dependency on gtest.
3537
option(BUILD_TESTS "Build GTest-based tests" ON)
3638

3739
# Turn this off to build only the library.
3840
option(BUILD_TOOLS "Build tools" ON)
3941

42+
option(BUILD_LLVM_DWARF "Enable full DWARF support" ON)
43+
if(EMSCRIPTEN)
44+
# For now, don't include full DWARF support in JS builds, for size.
45+
set(BUILD_LLVM_DWARF OFF)
46+
endif()
47+
48+
option(BUILD_STATIC_LIB "Build as a static library" OFF)
49+
if(MSVC)
50+
# We don't have dllexport declarations set up for windows yet.
51+
set(BUILD_STATIC_LIB ON)
52+
endif()
53+
4054
# Turn this off to install only tools and not static/dynamic libs
4155
option(INSTALL_LIBS "Install libraries" ON)
4256

@@ -61,6 +75,8 @@ option(EMSCRIPTEN_ENABLE_PTHREADS "Enable pthreads in emscripten build" OFF)
6175
# This is useful for debugging, performance analysis, and other testing.
6276
option(EMSCRIPTEN_ENABLE_SINGLE_FILE "Enable SINGLE_FILE mode in emscripten build" ON)
6377

78+
option(ENABLE_WERROR "Enable -Werror" ON)
79+
6480
# For git users, attempt to generate a more useful version string
6581
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git)
6682
find_package(Git QUIET REQUIRED)
@@ -80,6 +96,11 @@ endif()
8096

8197
configure_file(config.h.in config.h)
8298

99+
# Configure threads
100+
101+
set(THREADS_PREFER_PTHREAD_FLAG ON)
102+
find_package(Threads REQUIRED)
103+
83104
# Support functionality.
84105

85106
function(add_compile_flag value)
@@ -89,11 +110,6 @@ function(add_compile_flag value)
89110
endforeach(variable)
90111
endfunction()
91112

92-
function(add_cxx_flag value)
93-
message(STATUS "Building with ${value}")
94-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${value}" PARENT_SCOPE)
95-
endfunction()
96-
97113
function(add_debug_compile_flag value)
98114
if("${CMAKE_BUILD_TYPE}" MATCHES "Debug")
99115
message(STATUS "Building with ${value}")
@@ -145,33 +161,12 @@ endfunction()
145161

146162
function(binaryen_add_executable name sources)
147163
add_executable(${name} ${sources})
148-
target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT})
164+
target_link_libraries(${name} Threads::Threads)
149165
target_link_libraries(${name} binaryen)
150166
binaryen_setup_rpath(${name})
151167
install(TARGETS ${name} DESTINATION ${CMAKE_INSTALL_BINDIR})
152168
endfunction()
153169

154-
# Options
155-
156-
option(BUILD_STATIC_LIB "Build as a static library" OFF)
157-
if(MSVC)
158-
# We don't have dllexport declarations set up for windows yet.
159-
set(BUILD_STATIC_LIB ON)
160-
endif()
161-
162-
# For now, don't include full DWARF support in JS builds, for size.
163-
if(NOT EMSCRIPTEN)
164-
option(BUILD_LLVM_DWARF "Enable full DWARF support" ON)
165-
166-
if(BUILD_LLVM_DWARF)
167-
if(MSVC)
168-
ADD_COMPILE_FLAG("/DBUILD_LLVM_DWARF")
169-
else()
170-
ADD_COMPILE_FLAG("-DBUILD_LLVM_DWARF")
171-
endif()
172-
endif()
173-
endif()
174-
175170
# Compiler setup. Use SYSTEM to avoid warnings and errors from third-party headers.
176171

177172
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
@@ -183,14 +178,27 @@ endif()
183178
# Add output directory to include path so config.h can be found
184179
include_directories(${CMAKE_CURRENT_BINARY_DIR})
185180

181+
# Configure output locations.
182+
186183
# Force output to bin/ and lib/. This is to suppress CMake multigenerator output paths and avoid bin/Debug, bin/Release/ and so on, which is CMake default.
187184
foreach(SUFFIX "_DEBUG" "_RELEASE" "_RELWITHDEBINFO" "_MINSIZEREL" "")
188185
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/bin")
189186
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/lib")
190187
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/lib")
191188
endforeach()
192189

193-
option(BYN_ENABLE_LTO "Build with LTO" Off)
190+
# Compiler setup for both 1P and 3P code.
191+
192+
if(NOT MSVC AND NOT EMSCRIPTEN)
193+
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
194+
# wasm doesn't allow for x87 floating point math
195+
add_compile_flag("-msse2")
196+
add_compile_flag("-mfpmath=sse")
197+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^armv[2-6]" AND NOT CMAKE_CXX_FLAGS MATCHES "-mfpu=")
198+
add_compile_flag("-mfpu=vfpv3")
199+
endif()
200+
endif()
201+
194202
if(BYN_ENABLE_LTO)
195203
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
196204
message(FATAL_ERROR "ThinLTO is only supported by clang")
@@ -210,19 +218,10 @@ if(MSVC)
210218
add_compile_flag("/arch:sse2")
211219
endif()
212220
endif()
213-
add_compile_flag("/wd4146") # Ignore warning "warning C4146: unary minus operator applied to unsigned type, result still unsigned", this pattern is used somewhat commonly in the code.
214-
# 4267 and 4244 are conversion/truncation warnings. We might want to fix these but they are currently pervasive.
215-
add_compile_flag("/wd4267")
216-
add_compile_flag("/wd4244")
217-
# 4722 warns that destructors never return, even with [[noreturn]].
218-
add_compile_flag("/wd4722")
219-
# "destructor was implicitly defined as deleted" caused by LLVM headers.
220-
add_compile_flag("/wd4624")
221-
add_compile_flag("/WX-")
221+
222222
add_debug_compile_flag("/Od")
223223
add_nondebug_compile_flag("/O2")
224-
add_compile_flag("/D_CRT_SECURE_NO_WARNINGS")
225-
add_compile_flag("/D_SCL_SECURE_NO_WARNINGS")
224+
226225
# workaround for https://github.com/WebAssembly/binaryen/issues/3661
227226
add_compile_flag("/D_SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING")
228227
# Visual Studio 2018 15.8 implemented conformant support for std::aligned_storage, but the conformant support is only enabled when the following flag is passed, to avoid
@@ -257,51 +256,10 @@ if(MSVC)
257256

258257
add_link_flag("/STACK:8388608")
259258

260-
if(RUN_STATIC_ANALYZER)
261-
add_definitions(/analyze)
262-
endif()
263-
else()
259+
else() # MSVC
264260

265-
option(ENABLE_WERROR "Enable -Werror" ON)
266-
267-
set(THREADS_PREFER_PTHREAD_FLAG ON)
268-
set(CMAKE_THREAD_PREFER_PTHREAD ON)
269-
find_package(Threads REQUIRED)
270-
if(NOT EMSCRIPTEN)
271-
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
272-
# wasm doesn't allow for x87 floating point math
273-
add_compile_flag("-msse2")
274-
add_compile_flag("-mfpmath=sse")
275-
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^armv[2-6]" AND NOT CMAKE_CXX_FLAGS MATCHES "-mfpu=")
276-
add_compile_flag("-mfpu=vfpv3")
277-
endif()
278-
endif()
279-
add_compile_flag("-Wall")
280-
if(ENABLE_WERROR)
281-
add_compile_flag("-Werror")
282-
endif()
283-
add_compile_flag("-Wextra")
284-
add_compile_flag("-Wno-unused-parameter")
285-
add_compile_flag("-Wno-dangling-pointer") # false positive in gcc
286261
add_compile_flag("-fno-omit-frame-pointer")
287262
add_compile_flag("-fno-rtti")
288-
# TODO(https://github.com/WebAssembly/binaryen/pull/2314): Remove these two
289-
# flags once we resolve the issue.
290-
add_compile_flag("-Wno-implicit-int-float-conversion")
291-
add_compile_flag("-Wno-unknown-warning-option")
292-
add_compile_flag("-Wswitch") # we explicitly expect this in the code
293-
add_compile_flag("-Wimplicit-fallthrough")
294-
add_compile_flag("-Wnon-virtual-dtor")
295-
296-
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
297-
# Google style requires this, so make sure we compile cleanly with it.
298-
add_compile_flag("-Wctad-maybe-unsupported")
299-
# Disable a warning that started to happen on system headers (so we can't
300-
# fix it in our codebase) on github CI:
301-
# https://github.com/WebAssembly/binaryen/pull/6597
302-
add_compile_flag("-Wno-deprecated-declarations")
303-
endif()
304-
305263
if(WIN32)
306264
add_compile_flag("-D_GNU_SOURCE")
307265
add_compile_flag("-D__STDC_FORMAT_MACROS")
@@ -400,6 +358,69 @@ if(UNIX AND CMAKE_GENERATOR STREQUAL "Ninja")
400358
endif()
401359
endif()
402360

361+
# Add targets and sources for 3P code.
362+
363+
add_subdirectory(third_party)
364+
365+
# Additionally configure compiler for 1P code.
366+
367+
if(BUILD_LLVM_DWARF)
368+
if(MSVC)
369+
add_compile_flag("/DBUILD_LLVM_DWARF")
370+
else()
371+
add_compile_flag("-DBUILD_LLVM_DWARF")
372+
endif()
373+
endif()
374+
375+
# Configure warnings and errors
376+
377+
if(MSVC)
378+
add_compile_flag("/wd4146") # Ignore warning "warning C4146: unary minus operator applied to unsigned type, result still unsigned", this pattern is used somewhat commonly in the code.
379+
# 4267 and 4244 are conversion/truncation warnings. We might want to fix these but they are currently pervasive.
380+
add_compile_flag("/wd4267")
381+
add_compile_flag("/wd4244")
382+
# 4722 warns that destructors never return, even with [[noreturn]].
383+
add_compile_flag("/wd4722")
384+
# "destructor was implicitly defined as deleted" caused by LLVM headers.
385+
add_compile_flag("/wd4624")
386+
add_compile_flag("/WX-")
387+
add_compile_flag("/D_CRT_SECURE_NO_WARNINGS")
388+
add_compile_flag("/D_SCL_SECURE_NO_WARNINGS")
389+
390+
if(RUN_STATIC_ANALYZER)
391+
add_definitions(/analyze)
392+
endif()
393+
394+
else() # MSVC
395+
396+
add_compile_flag("-Wall")
397+
if(ENABLE_WERROR)
398+
add_compile_flag("-Werror")
399+
endif()
400+
add_compile_flag("-Wextra")
401+
add_compile_flag("-Wno-unused-parameter")
402+
add_compile_flag("-Wno-dangling-pointer") # false positive in gcc
403+
# TODO(https://github.com/WebAssembly/binaryen/pull/2314): Remove these two
404+
# flags once we resolve the issue.
405+
add_compile_flag("-Wno-implicit-int-float-conversion")
406+
add_compile_flag("-Wno-unknown-warning-option")
407+
add_compile_flag("-Wswitch") # we explicitly expect this in the code
408+
add_compile_flag("-Wimplicit-fallthrough")
409+
add_compile_flag("-Wnon-virtual-dtor")
410+
411+
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
412+
# Google style requires this, so make sure we compile cleanly with it.
413+
add_compile_flag("-Wctad-maybe-unsupported")
414+
# Disable a warning that started to happen on system headers (so we can't
415+
# fix it in our codebase) on github CI:
416+
# https://github.com/WebAssembly/binaryen/pull/6597
417+
add_compile_flag("-Wno-deprecated-declarations")
418+
endif()
419+
420+
endif()
421+
422+
# Declare libbinaryen
423+
403424
if(BUILD_STATIC_LIB)
404425
message(STATUS "Building libbinaryen as statically linked library.")
405426
add_library(binaryen STATIC)
@@ -408,6 +429,10 @@ else()
408429
message(STATUS "Building libbinaryen as shared library.")
409430
add_library(binaryen SHARED)
410431
endif()
432+
target_link_libraries(binaryen Threads::Threads)
433+
if(BUILD_LLVM_DWARF)
434+
target_link_libraries(binaryen llvm_dwarf)
435+
endif()
411436

412437
add_subdirectory(src/ir)
413438
add_subdirectory(src/asmjs)
@@ -425,8 +450,6 @@ if(BUILD_TOOLS)
425450
add_subdirectory(src/tools)
426451
endif()
427452

428-
add_subdirectory(third_party)
429-
430453
# Configure lit tests
431454
add_subdirectory(test/lit)
432455

@@ -443,7 +466,6 @@ set(binaryen_SOURCES
443466
${binaryen_HEADERS}
444467
)
445468
target_sources(binaryen PRIVATE ${binaryen_SOURCES})
446-
target_link_libraries(binaryen ${CMAKE_THREAD_LIBS_INIT})
447469

448470
if(INSTALL_LIBS OR NOT BUILD_STATIC_LIB)
449471
install(TARGETS binaryen
@@ -459,12 +481,9 @@ endif()
459481
#
460482
# Note that we can't emit binaryen.js directly, as there is libbinaryen already
461483
# declared earlier, so we create binaryen_wasm/js.js, which must then be copied.
462-
# Note that SHELL: is needed as otherwise cmake will coalesce -s link flags
463-
# in an incorrect way for emscripten.
464484
if(EMSCRIPTEN)
465485
# binaryen.js WebAssembly variant
466-
add_executable(binaryen_wasm
467-
${binaryen_SOURCES})
486+
add_executable(binaryen_wasm ${binaryen_SOURCES})
468487
target_link_libraries(binaryen_wasm binaryen)
469488
target_link_libraries(binaryen_wasm "-sFILESYSTEM")
470489
target_link_libraries(binaryen_wasm "-sEXPORT_NAME=Binaryen")
@@ -493,8 +512,7 @@ if(EMSCRIPTEN)
493512
install(TARGETS binaryen_wasm DESTINATION ${CMAKE_INSTALL_BINDIR})
494513

495514
# binaryen.js JavaScript variant
496-
add_executable(binaryen_js
497-
${binaryen_SOURCES})
515+
add_executable(binaryen_js ${binaryen_SOURCES})
498516
target_link_libraries(binaryen_js binaryen)
499517
target_link_libraries(binaryen_js "-sWASM=0")
500518
target_link_libraries(binaryen_js "-sWASM_ASYNC_COMPILATION=0")

test/gtest/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
include_directories(../../third_party/googletest/googletest/include)
2-
include_directories(../../src/wasm)
1+
include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/third_party/googletest/googletest/include)
32

43
set(unittest_SOURCES
54
arena.cpp

third_party/llvm-project/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,3 @@ SET(llvm_dwarf_SOURCES
7474
YAMLTraits.cpp
7575
)
7676
add_library(llvm_dwarf OBJECT ${llvm_dwarf_SOURCES})
77-
target_link_libraries(binaryen llvm_dwarf)

0 commit comments

Comments
 (0)