From bec67eb50209a711c2f5a9716bbc8e6080cd3657 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Tue, 21 Jan 2025 16:51:32 -0800 Subject: [PATCH 01/10] Apply custom compilation flags only to 1P code Our CMakeLists.txt previously added compile and link flags by updating the CMAKE_CXX_FLAGS and similar global properties. This caused problems when experimenting with adding new dependencies to Binaryen because our custom compile flags, which largely enable stricter errors, were incompatible with the third party code. Simplify and improve our CMake configuration by applying extra compilation flags only to code under our control. Do so by coalescing all of the libbinaryen sources into a single target, then use CMake's target_compile_options and target_link_options commands to set the flags only for the libbinaryen and tool targets. Also update the minimum required CMake version to avoid a policy error. --- CMakeLists.txt | 167 +++++++++++------------- src/analysis/CMakeLists.txt | 2 +- src/asmjs/CMakeLists.txt | 2 +- src/cfg/CMakeLists.txt | 2 +- src/emscripten-optimizer/CMakeLists.txt | 2 +- src/ir/CMakeLists.txt | 2 +- src/parser/CMakeLists.txt | 2 +- src/passes/CMakeLists.txt | 2 +- src/support/CMakeLists.txt | 4 +- src/wasm/CMakeLists.txt | 4 +- third_party/llvm-project/CMakeLists.txt | 8 +- 11 files changed, 91 insertions(+), 106 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d02096fa06e..da9e9d81e74 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 @@ -82,41 +82,35 @@ configure_file(config.h.in config.h) # 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) @@ -149,8 +143,65 @@ function(binaryen_add_executable name sources) 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() +# 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() + +# 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}) + +# 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. +foreach(SUFFIX "_DEBUG" "_RELEASE" "_RELWITHDEBINFO" "_MINSIZEREL" "") + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/bin") + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/lib") + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/lib") +endforeach() + +# 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/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() + # Options option(BUILD_STATIC_LIB "Build as a static library" OFF) @@ -174,22 +225,6 @@ endif() # Compiler setup. -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}) - -# 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. -foreach(SUFFIX "_DEBUG" "_RELEASE" "_RELWITHDEBINFO" "_MINSIZEREL" "") - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/bin") - set(CMAKE_LIBRARY_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/lib") - set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/lib") -endforeach() - option(BYN_ENABLE_LTO "Build with LTO" Off) if(BYN_ENABLE_LTO) if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") @@ -310,8 +345,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,51 +433,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/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) @@ -452,14 +440,7 @@ 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_sources(binaryen PRIVATE ${binaryen_SOURCES}) target_link_libraries(binaryen ${CMAKE_THREAD_LIBS_INIT}) if(INSTALL_LIBS OR NOT BUILD_STATIC_LIB) install(TARGETS binaryen 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/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/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 $) From 5741be9b48129ca2041274283c53f54c84ab8c2d Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Tue, 21 Jan 2025 20:54:37 -0800 Subject: [PATCH 02/10] add llvm include directory for binaryen-unittest --- test/gtest/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/test/gtest/CMakeLists.txt b/test/gtest/CMakeLists.txt index 102d3ca2a48..f94af4d4f3f 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 From 24b576d98843f3de7b2ab8250bbd84e2e201982c Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Tue, 21 Jan 2025 21:01:25 -0800 Subject: [PATCH 03/10] Declare include directories first --- CMakeLists.txt | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index da9e9d81e74..385c4a0a111 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -146,6 +146,16 @@ function(binaryen_add_executable name sources) set_property(GLOBAL APPEND PROPERTY binaryen_1p_targets ${name}) endfunction() +# 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}) + # Declare libbinaryen if(BUILD_STATIC_LIB) @@ -157,17 +167,6 @@ else() add_library(binaryen SHARED) endif() -# 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}) - # 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. foreach(SUFFIX "_DEBUG" "_RELEASE" "_RELWITHDEBINFO" "_MINSIZEREL" "") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/bin") From 94c95f0452b5efb2fb7b587cdb7b190dfa032f13 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Tue, 21 Jan 2025 21:51:00 -0800 Subject: [PATCH 04/10] move some options up --- CMakeLists.txt | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 385c4a0a111..b15989e6906 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -203,16 +214,8 @@ endif() # 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") @@ -224,7 +227,6 @@ endif() # Compiler setup. -option(BYN_ENABLE_LTO "Build with LTO" Off) if(BYN_ENABLE_LTO) if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") message(FATAL_ERROR "ThinLTO is only supported by clang") From 88e1d45fefecdeb82b6f52058765d6d42dfd3bde Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Wed, 22 Jan 2025 11:58:53 -0800 Subject: [PATCH 05/10] move library output directory config up --- CMakeLists.txt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b15989e6906..4dfbec80479 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -167,6 +167,13 @@ endif() # Add output directory to include path so config.h can be found include_directories(${CMAKE_CURRENT_BINARY_DIR}) +# 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. +foreach(SUFFIX "_DEBUG" "_RELEASE" "_RELWITHDEBINFO" "_MINSIZEREL" "") + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/bin") + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/lib") + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/lib") +endforeach() + # Declare libbinaryen if(BUILD_STATIC_LIB) @@ -178,13 +185,6 @@ else() add_library(binaryen SHARED) endif() -# 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. -foreach(SUFFIX "_DEBUG" "_RELEASE" "_RELWITHDEBINFO" "_MINSIZEREL" "") - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/bin") - set(CMAKE_LIBRARY_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/lib") - set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/lib") -endforeach() - # Add sources and declare executable targets add_subdirectory(src/ir) From c6a0749c16c046819e9670ae70625324a7dd35a5 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Wed, 22 Jan 2025 13:39:47 -0800 Subject: [PATCH 06/10] fix js and wasm links --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 21688429bf9..02df7790d5f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -464,7 +464,7 @@ 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") @@ -494,7 +494,7 @@ if(EMSCRIPTEN) # 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") From 96d74540d8fa3d60764419843b3e95263a7d1896 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Wed, 22 Jan 2025 17:28:55 -0800 Subject: [PATCH 07/10] declare binaryen_js and binaryen_wasm early --- CMakeLists.txt | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 02df7790d5f..e3f47532810 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -213,7 +213,22 @@ if(BUILD_TESTS) add_subdirectory(test/gtest) endif() -# Options +# 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) @@ -435,15 +450,6 @@ if(UNIX AND CMAKE_GENERATOR STREQUAL "Ninja") endif() 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}) if(INSTALL_LIBS OR NOT BUILD_STATIC_LIB) install(TARGETS binaryen RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} @@ -462,8 +468,6 @@ endif() # in an incorrect way for emscripten. if(EMSCRIPTEN) # binaryen.js WebAssembly variant - add_executable(binaryen_wasm - ${binaryen_SOURCES}) target_link_libraries(binaryen_wasm binaryen) target_link_libraries(binaryen_wasm "-sFILESYSTEM") target_link_libraries(binaryen_wasm "-sEXPORT_NAME=Binaryen") @@ -492,8 +496,6 @@ 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 binaryen) target_link_libraries(binaryen_js "-sWASM=0") target_link_libraries(binaryen_js "-sWASM_ASYNC_COMPILATION=0") From 3714bb11b3df8fb2a9fe422f69c8a402613f8f5c Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Wed, 22 Jan 2025 18:59:16 -0800 Subject: [PATCH 08/10] add -fno-rtti to gtest --- third_party/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) 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() From 16f44020800354479f1a5426109c07f59d38c557 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Wed, 22 Jan 2025 21:02:14 -0800 Subject: [PATCH 09/10] modernize and hoist thread config --- CMakeLists.txt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e3f47532810..1c40db26170 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -91,6 +91,11 @@ endif() configure_file(config.h.in config.h) +# Configure threads + +set(THREADS_PREFER_PTHREAD_FLAG ON) +find_package(Threads REQUIRED) + # Support functionality. # Use a property to emulate a global variable. @@ -150,7 +155,7 @@ 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}) @@ -316,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 From 9324e745606d70b2e9bd16643f2e779f9345c688 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Thu, 23 Jan 2025 09:16:41 -0800 Subject: [PATCH 10/10] Off -> OFF, On -> ON --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c40db26170..ae18e9fc2bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,7 +49,7 @@ if(MSVC) set(BUILD_STATIC_LIB ON) endif() -option(BYN_ENABLE_LTO "Build with LTO" Off) +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) @@ -148,7 +148,7 @@ 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()