diff --git a/CMakeLists.txt b/CMakeLists.txt index c7e3699f7f1..ae18e9fc2bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ -# Version set according the the cmake versions available in Ubuntu/Bionic: -# https://packages.ubuntu.com/bionic/cmake -cmake_minimum_required(VERSION 3.10.2) +# Version set according the the cmake versions available in Ubuntu/Focal: +# https://packages.ubuntu.com/focal/cmake +cmake_minimum_required(VERSION 3.16.3) # Needed for C++17 (std::variant) # TODO(https://github.com/WebAssembly/binaryen/issues/4299): We need @@ -37,12 +37,23 @@ option(BUILD_TESTS "Build GTest-based tests" ON) # Turn this off to build only the library. option(BUILD_TOOLS "Build tools" ON) -# Turn this off to install only tools and not static/dynamic libs -option(INSTALL_LIBS "Install libraries" ON) - # Turn this on to build only the subset of tools needed for emscripten option(BUILD_EMSCRIPTEN_TOOLS_ONLY "Build only tools needed by emscripten" OFF) +# Build LLVM's DWARF support. This is unconditionally disabled when building with Emscripten. +option(BUILD_LLVM_DWARF "Enable full DWARF support" ON) + +option(BUILD_STATIC_LIB "Build as a static library" OFF) +if(MSVC) + # We don't have dllexport declarations set up for windows yet. + set(BUILD_STATIC_LIB ON) +endif() + +option(BYN_ENABLE_LTO "Build with LTO" OFF) + +# Turn this off to install only tools and not static/dynamic libs +option(INSTALL_LIBS "Install libraries" ON) + # Turn this on to build binaryen.js as ES5, with additional compatibility configuration for js_of_ocaml. option(JS_OF_OCAML "Build binaryen.js for js_of_ocaml" OFF) @@ -80,43 +91,42 @@ endif() configure_file(config.h.in config.h) +# Configure threads + +set(THREADS_PREFER_PTHREAD_FLAG ON) +find_package(Threads REQUIRED) + # Support functionality. -function(add_compile_flag value) - message(STATUS "Building with ${value}") - foreach(variable CMAKE_C_FLAGS CMAKE_CXX_FLAGS) - set(${variable} "${${variable}} ${value}" PARENT_SCOPE) - endforeach(variable) -endfunction() +# Use a property to emulate a global variable. +set_property(GLOBAL PROPERTY binaryen_1p_targets binaryen) -function(add_cxx_flag value) +function(add_compile_flag value) message(STATUS "Building with ${value}") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${value}" PARENT_SCOPE) + get_property(targets GLOBAL PROPERTY binaryen_1p_targets) + foreach(target ${targets}) + target_compile_options(${target} PRIVATE ${value}) + endforeach() endfunction() function(add_debug_compile_flag value) if("${CMAKE_BUILD_TYPE}" MATCHES "Debug") - message(STATUS "Building with ${value}") + add_compile_flag(${value}) endif() - foreach(variable CMAKE_C_FLAGS_DEBUG CMAKE_CXX_FLAGS_DEBUG) - set(${variable} "${${variable}} ${value}" PARENT_SCOPE) - endforeach(variable) endfunction() function(add_nondebug_compile_flag value) if(NOT "${CMAKE_BUILD_TYPE}" MATCHES "Debug") - message(STATUS "Building with ${value}") + add_compile_flag(${value}) endif() - foreach(variable CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS_RELWITHDEBINFO CMAKE_CXX_FLAGS_RELWITHDEBINFO CMAKE_C_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_MINSIZEREL) - set(${variable} "${${variable}} ${value}" PARENT_SCOPE) - endforeach(variable) endfunction() function(add_link_flag value) message(STATUS "Linking with ${value}") - foreach(variable CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) - set(${variable} "${${variable}} ${value}" PARENT_SCOPE) - endforeach(variable) + get_property(targets GLOBAL PROPERTY binaryen_1p_targets) + foreach(target ${targets}) + target_link_options(${target} PRIVATE ${value}) + endforeach() endfunction() function(binaryen_setup_rpath name) @@ -138,48 +148,27 @@ function(binaryen_setup_rpath name) endif() set_target_properties(${name} PROPERTIES - BUILD_WITH_INSTALL_RPATH On + BUILD_WITH_INSTALL_RPATH ON INSTALL_RPATH "${_install_rpath}" ${_install_name_dir}) endfunction() function(binaryen_add_executable name sources) add_executable(${name} ${sources}) - target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT}) + target_link_libraries(${name} Threads::Threads) target_link_libraries(${name} binaryen) binaryen_setup_rpath(${name}) install(TARGETS ${name} DESTINATION ${CMAKE_INSTALL_BINDIR}) + set_property(GLOBAL APPEND PROPERTY binaryen_1p_targets ${name}) endfunction() -# Options - -option(BUILD_STATIC_LIB "Build as a static library" OFF) -if(MSVC) - # We don't have dllexport declarations set up for windows yet. - set(BUILD_STATIC_LIB ON) -endif() - -# For now, don't include full DWARF support in JS builds, for size. -if(NOT EMSCRIPTEN) - option(BUILD_LLVM_DWARF "Enable full DWARF support" ON) - - if(BUILD_LLVM_DWARF) - if(MSVC) - ADD_COMPILE_FLAG("/DBUILD_LLVM_DWARF") - else() - ADD_COMPILE_FLAG("-DBUILD_LLVM_DWARF") - endif() - endif() -endif() - -# Compiler setup. +# Set include paths include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third_party/FP16/include) if(BUILD_LLVM_DWARF) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/include) endif() - # Add output directory to include path so config.h can be found include_directories(${CMAKE_CURRENT_BINARY_DIR}) @@ -190,7 +179,75 @@ foreach(SUFFIX "_DEBUG" "_RELEASE" "_RELWITHDEBINFO" "_MINSIZEREL" "") set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/lib") endforeach() -option(BYN_ENABLE_LTO "Build with LTO" Off) +# Declare libbinaryen + +if(BUILD_STATIC_LIB) + message(STATUS "Building libbinaryen as statically linked library.") + add_library(binaryen STATIC) + add_definitions(-DBUILD_STATIC_LIBRARY) +else() + message(STATUS "Building libbinaryen as shared library.") + add_library(binaryen SHARED) +endif() + +# Add sources and declare executable targets + +add_subdirectory(src/ir) +add_subdirectory(src/asmjs) +add_subdirectory(src/cfg) +add_subdirectory(src/emscripten-optimizer) +add_subdirectory(src/interpreter) +add_subdirectory(src/passes) +add_subdirectory(src/parser) +add_subdirectory(src/support) +add_subdirectory(src/wasm) +add_subdirectory(src/analysis) + +if(BUILD_TOOLS) + # Build binaryen tools + add_subdirectory(src/tools) +endif() + +add_subdirectory(third_party) + +# Configure lit tests +add_subdirectory(test/lit) + +if(BUILD_TESTS) + # Configure GTest unit tests + add_subdirectory(test/gtest) +endif() + +# Sources. + +file(GLOB binaryen_HEADERS src/*.h) +set(binaryen_SOURCES + src/binaryen-c.cpp + ${binaryen_HEADERS} +) +target_sources(binaryen PRIVATE ${binaryen_SOURCES}) +target_link_libraries(binaryen ${CMAKE_THREAD_LIBS_INIT}) + +# Declare binaryen.js targets + +if(EMSCRIPTEN) + binaryen_add_executable(binaryen_wasm ${binaryen_SOURCES}) + binaryen_add_executable(binaryen_js ${binaryen_SOURCES}) +endif() + +# For now, don't include full DWARF support in JS builds, for size. +if(NOT EMSCRIPTEN) + if(BUILD_LLVM_DWARF) + if(MSVC) + ADD_COMPILE_FLAG("/DBUILD_LLVM_DWARF") + else() + ADD_COMPILE_FLAG("-DBUILD_LLVM_DWARF") + endif() + endif() +endif() + +# Compiler setup. + if(BYN_ENABLE_LTO) if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") message(FATAL_ERROR "ThinLTO is only supported by clang") @@ -264,9 +321,6 @@ else() option(ENABLE_WERROR "Enable -Werror" ON) - set(THREADS_PREFER_PTHREAD_FLAG ON) - set(CMAKE_THREAD_PREFER_PTHREAD ON) - find_package(Threads REQUIRED) if(NOT EMSCRIPTEN) if(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$") # wasm doesn't allow for x87 floating point math @@ -310,8 +364,6 @@ else() else() add_link_flag("-Wl,--stack,8388608") endif() - elseif(NOT EMSCRIPTEN) - add_compile_flag("-fPIC") endif() add_debug_compile_flag("-g3") if(BYN_ENABLE_ASSERTIONS) @@ -400,69 +452,6 @@ if(UNIX AND CMAKE_GENERATOR STREQUAL "Ninja") endif() endif() -# Static libraries -# Current (partial) dependency structure is as follows: -# tools -> passes -> wasm -> asmjs -> support -# TODO: It's odd that wasm should depend on asmjs, maybe we should fix that. -add_subdirectory(src/ir) -add_subdirectory(src/asmjs) -add_subdirectory(src/cfg) -add_subdirectory(src/emscripten-optimizer) -add_subdirectory(src/interpreter) -add_subdirectory(src/passes) -add_subdirectory(src/parser) -add_subdirectory(src/support) -add_subdirectory(src/wasm) -add_subdirectory(src/analysis) - -if(BUILD_TOOLS) - # Build binaryen tools - add_subdirectory(src/tools) -endif() - -add_subdirectory(third_party) - -# Configure lit tests -add_subdirectory(test/lit) - -if(BUILD_TESTS) - # Configure GTest unit tests - add_subdirectory(test/gtest) -endif() - -# Object files -set(binaryen_objs - $ - $ - $ - $ - $ - $ - $ - $ - $ - $) - -if(BUILD_LLVM_DWARF) - SET(binaryen_objs ${binaryen_objs} $) -endif() - -# Sources. - -file(GLOB binaryen_HEADERS src/*.h) -set(binaryen_SOURCES - src/binaryen-c.cpp - ${binaryen_HEADERS} -) -if(BUILD_STATIC_LIB) - message(STATUS "Building libbinaryen as statically linked library.") - add_library(binaryen STATIC ${binaryen_SOURCES} ${binaryen_objs}) - add_definitions(-DBUILD_STATIC_LIBRARY) -else() - message(STATUS "Building libbinaryen as shared library.") - add_library(binaryen SHARED ${binaryen_SOURCES} ${binaryen_objs}) -endif() -target_link_libraries(binaryen ${CMAKE_THREAD_LIBS_INIT}) if(INSTALL_LIBS OR NOT BUILD_STATIC_LIB) install(TARGETS binaryen RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} @@ -481,9 +470,7 @@ endif() # in an incorrect way for emscripten. if(EMSCRIPTEN) # binaryen.js WebAssembly variant - add_executable(binaryen_wasm - ${binaryen_SOURCES}) - target_link_libraries(binaryen_wasm wasm asmjs emscripten-optimizer passes ir cfg support analysis parser interpreter wasm) + target_link_libraries(binaryen_wasm binaryen) target_link_libraries(binaryen_wasm "-sFILESYSTEM") target_link_libraries(binaryen_wasm "-sEXPORT_NAME=Binaryen") target_link_libraries(binaryen_wasm "-sNODERAWFS=0") @@ -511,9 +498,7 @@ if(EMSCRIPTEN) install(TARGETS binaryen_wasm DESTINATION ${CMAKE_INSTALL_BINDIR}) # binaryen.js JavaScript variant - add_executable(binaryen_js - ${binaryen_SOURCES}) - target_link_libraries(binaryen_js wasm asmjs emscripten-optimizer passes ir cfg support analysis parser interpreter wasm) + target_link_libraries(binaryen_js binaryen) target_link_libraries(binaryen_js "-sWASM=0") target_link_libraries(binaryen_js "-sWASM_ASYNC_COMPILATION=0") if(${CMAKE_CXX_COMPILER_VERSION} STREQUAL "6.0.1") diff --git a/src/analysis/CMakeLists.txt b/src/analysis/CMakeLists.txt index 313eec0d5d3..8b843db5373 100644 --- a/src/analysis/CMakeLists.txt +++ b/src/analysis/CMakeLists.txt @@ -3,4 +3,4 @@ set(analysis_SOURCES cfg.cpp ${analysis_HEADERS} ) -add_library(analysis OBJECT ${analysis_SOURCES}) +target_sources(binaryen PRIVATE ${analysis_SOURCES}) diff --git a/src/asmjs/CMakeLists.txt b/src/asmjs/CMakeLists.txt index d677f1bfe81..9f80aa7d399 100644 --- a/src/asmjs/CMakeLists.txt +++ b/src/asmjs/CMakeLists.txt @@ -5,4 +5,4 @@ set(asmjs_SOURCES shared-constants.cpp ${asmjs_HEADERS} ) -add_library(asmjs OBJECT ${asmjs_SOURCES}) +target_sources(binaryen PRIVATE ${asmjs_SOURCES}) diff --git a/src/cfg/CMakeLists.txt b/src/cfg/CMakeLists.txt index 71d77f0b03e..175d5fbb7b9 100644 --- a/src/cfg/CMakeLists.txt +++ b/src/cfg/CMakeLists.txt @@ -3,4 +3,4 @@ set(cfg_SOURCES Relooper.cpp ${cfg_HEADERS} ) -add_library(cfg OBJECT ${cfg_SOURCES}) +target_sources(binaryen PRIVATE ${cfg_SOURCES}) diff --git a/src/emscripten-optimizer/CMakeLists.txt b/src/emscripten-optimizer/CMakeLists.txt index 391c1444f7a..127954248d7 100644 --- a/src/emscripten-optimizer/CMakeLists.txt +++ b/src/emscripten-optimizer/CMakeLists.txt @@ -5,4 +5,4 @@ set(emscripten-optimizer_SOURCES simple_ast.cpp ${emscripten-optimizer_HEADERS} ) -add_library(emscripten-optimizer OBJECT ${emscripten-optimizer_SOURCES}) +target_sources(binaryen PRIVATE ${emscripten-optimizer_SOURCES}) diff --git a/src/interpreter/CMakeLists.txt b/src/interpreter/CMakeLists.txt index 170376476d5..dd0b038f650 100644 --- a/src/interpreter/CMakeLists.txt +++ b/src/interpreter/CMakeLists.txt @@ -4,4 +4,4 @@ set(interpreter_SOURCES interpreter.cpp ${interpreter_HEADERS} ) -add_library(interpreter OBJECT ${interpreter_SOURCES}) +target_sources(binaryen PRIVATE ${interpreter_SOURCES}) diff --git a/src/ir/CMakeLists.txt b/src/ir/CMakeLists.txt index 45b08702de8..fa2ee1127d7 100644 --- a/src/ir/CMakeLists.txt +++ b/src/ir/CMakeLists.txt @@ -24,4 +24,4 @@ set(ir_SOURCES module-splitting.cpp ${ir_HEADERS} ) -add_library(ir OBJECT ${ir_SOURCES}) +target_sources(binaryen PRIVATE ${ir_SOURCES}) diff --git a/src/parser/CMakeLists.txt b/src/parser/CMakeLists.txt index 045948ba1bb..8b7846ca9e9 100644 --- a/src/parser/CMakeLists.txt +++ b/src/parser/CMakeLists.txt @@ -12,4 +12,4 @@ set(parser_SOURCES wat-parser.cpp ${parser_HEADERS} ) -add_library(parser OBJECT ${parser_SOURCES}) +target_sources(binaryen PRIVATE ${parser_SOURCES}) diff --git a/src/passes/CMakeLists.txt b/src/passes/CMakeLists.txt index ed816ba09d4..83a17d36608 100644 --- a/src/passes/CMakeLists.txt +++ b/src/passes/CMakeLists.txt @@ -144,4 +144,4 @@ if(EMSCRIPTEN) list(REMOVE_ITEM passes_SOURCES "hash-stringify-walker.cpp") list(REMOVE_ITEM passes_SOURCES "Outlining.cpp") endif() -add_library(passes OBJECT ${passes_SOURCES}) +target_sources(binaryen PRIVATE ${passes_SOURCES}) diff --git a/src/support/CMakeLists.txt b/src/support/CMakeLists.txt index 54ee7b206e4..5c793662e1c 100644 --- a/src/support/CMakeLists.txt +++ b/src/support/CMakeLists.txt @@ -22,12 +22,12 @@ set(support_SOURCES # suffix_tree_node source files no longer depend on LLVM code in the # third_party folder if(EMSCRIPTEN) - add_library(support OBJECT ${support_SOURCES}) + target_sources(binaryen PRIVATE ${support_SOURCES}) else() set(support_with_suffix_tree_SOURCES suffix_tree.cpp suffix_tree_node.cpp ${support_SOURCES} ) - add_library(support OBJECT ${support_with_suffix_tree_SOURCES}) + target_sources(binaryen PRIVATE ${support_with_suffix_tree_SOURCES}) endif() diff --git a/src/wasm/CMakeLists.txt b/src/wasm/CMakeLists.txt index 64c88c99723..645655bb3c0 100644 --- a/src/wasm/CMakeLists.txt +++ b/src/wasm/CMakeLists.txt @@ -19,6 +19,6 @@ set(wasm_SOURCES ) # wasm-debug.cpp includes LLVM header using std::iterator (deprecated in C++17) if (NOT MSVC) - set_source_files_properties(wasm-debug.cpp PROPERTIES COMPILE_FLAGS -Wno-deprecated-declarations) + set_source_files_properties(wasm-debug.cpp TARGET_DIRECTORY binaryen PROPERTIES COMPILE_FLAGS -Wno-deprecated-declarations) endif() -add_library(wasm OBJECT ${wasm_SOURCES}) +target_sources(binaryen PRIVATE ${wasm_SOURCES}) diff --git a/test/gtest/CMakeLists.txt b/test/gtest/CMakeLists.txt index 41c2d4d0097..e5d488900e2 100644 --- a/test/gtest/CMakeLists.txt +++ b/test/gtest/CMakeLists.txt @@ -1,4 +1,5 @@ include_directories(../../third_party/googletest/googletest/include) +include_directories(../../third_party/llvm-project/include) include_directories(../../src/wasm) set(unittest_SOURCES diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt index b55797db770..b1259a5f6a4 100644 --- a/third_party/CMakeLists.txt +++ b/third_party/CMakeLists.txt @@ -10,4 +10,6 @@ include_directories( if(BUILD_TESTS) add_library(gtest STATIC googletest/googletest/src/gtest-all.cc) add_library(gtest_main STATIC googletest/googletest/src/gtest_main.cc) + target_compile_options(gtest PRIVATE "-fno-rtti") + target_compile_options(gtest_main PRIVATE "-fno-rtti") endif() diff --git a/third_party/llvm-project/CMakeLists.txt b/third_party/llvm-project/CMakeLists.txt index d338dd6e0c5..162d93583cd 100644 --- a/third_party/llvm-project/CMakeLists.txt +++ b/third_party/llvm-project/CMakeLists.txt @@ -63,7 +63,7 @@ SET(llvm_dwarf_SOURCES raw_ostream.cpp ScopedPrinter.cpp SmallVector.cpp - SourceMgr.cpp + SourceMgr.cpp StringMap.cpp StringRef.cpp SymbolicFile.cpp @@ -73,4 +73,8 @@ SET(llvm_dwarf_SOURCES YAMLParser.cpp # XXX needed? YAMLTraits.cpp ) -ADD_LIBRARY(llvm_dwarf OBJECT ${llvm_dwarf_SOURCES}) +add_library(llvm_dwarf OBJECT ${llvm_dwarf_SOURCES}) +if(NOT BUILD_STATIC_LIB) + set_property(TARGET llvm_dwarf PROPERTY POSITION_INDEPENDENT_CODE ON) +endif() +target_sources(binaryen PRIVATE $)