Skip to content

Commit

Permalink
Simplify cmake by having a single binaryen library target (#7238)
Browse files Browse the repository at this point in the history
Instead of declaring a separate static library target for each
subdirectory, declare a single binaryen library target up front and then
add sources to it from each subdirectory.

Requires updating the minimum cmake version to avoid policy errors.
  • Loading branch information
tlively authored Jan 23, 2025
1 parent 81035b0 commit 7074d87
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 53 deletions.
54 changes: 18 additions & 36 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
# 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
Expand Down Expand Up @@ -172,12 +172,12 @@ if(NOT EMSCRIPTEN)
endif()
endif()

# Compiler setup.
# Compiler setup. Use SYSTEM to avoid warnings and errors from third-party headers.

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third_party/FP16/include)
include_directories(SYSTEM ${CMAKE_CURRENT_SOURCE_DIR}/third_party/FP16/include)
if(BUILD_LLVM_DWARF)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/include)
include_directories(SYSTEM ${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/include)
endif()

# Add output directory to include path so config.h can be found
Expand Down Expand Up @@ -400,10 +400,15 @@ 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.
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_subdirectory(src/ir)
add_subdirectory(src/asmjs)
add_subdirectory(src/cfg)
Expand All @@ -430,39 +435,16 @@ if(BUILD_TESTS)
add_subdirectory(test/gtest)
endif()

# Object files
set(binaryen_objs
$<TARGET_OBJECTS:passes>
$<TARGET_OBJECTS:wasm>
$<TARGET_OBJECTS:asmjs>
$<TARGET_OBJECTS:emscripten-optimizer>
$<TARGET_OBJECTS:ir>
$<TARGET_OBJECTS:cfg>
$<TARGET_OBJECTS:support>
$<TARGET_OBJECTS:analysis>
$<TARGET_OBJECTS:parser>
$<TARGET_OBJECTS:interpreter>)

if(BUILD_LLVM_DWARF)
SET(binaryen_objs ${binaryen_objs} $<TARGET_OBJECTS:llvm_dwarf>)
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_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}
Expand All @@ -483,7 +465,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")
Expand Down Expand Up @@ -513,7 +495,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")
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ set(analysis_SOURCES
cfg.cpp
${analysis_HEADERS}
)
add_library(analysis OBJECT ${analysis_SOURCES})
target_sources(binaryen PRIVATE ${analysis_SOURCES})
2 changes: 1 addition & 1 deletion src/asmjs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ set(asmjs_SOURCES
shared-constants.cpp
${asmjs_HEADERS}
)
add_library(asmjs OBJECT ${asmjs_SOURCES})
target_sources(binaryen PRIVATE ${asmjs_SOURCES})
2 changes: 1 addition & 1 deletion src/cfg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ set(cfg_SOURCES
Relooper.cpp
${cfg_HEADERS}
)
add_library(cfg OBJECT ${cfg_SOURCES})
target_sources(binaryen PRIVATE ${cfg_SOURCES})
2 changes: 1 addition & 1 deletion src/emscripten-optimizer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
2 changes: 1 addition & 1 deletion src/interpreter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ set(interpreter_SOURCES
interpreter.cpp
${interpreter_HEADERS}
)
add_library(interpreter OBJECT ${interpreter_SOURCES})
target_sources(binaryen PRIVATE ${interpreter_SOURCES})
2 changes: 1 addition & 1 deletion src/ir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ set(ir_SOURCES
module-splitting.cpp
${ir_HEADERS}
)
add_library(ir OBJECT ${ir_SOURCES})
target_sources(binaryen PRIVATE ${ir_SOURCES})
2 changes: 1 addition & 1 deletion src/parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ set(parser_SOURCES
wat-parser.cpp
${parser_HEADERS}
)
add_library(parser OBJECT ${parser_SOURCES})
target_sources(binaryen PRIVATE ${parser_SOURCES})
2 changes: 1 addition & 1 deletion src/passes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
4 changes: 2 additions & 2 deletions src/support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
6 changes: 1 addition & 5 deletions src/wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,4 @@ set(wasm_SOURCES
wasm-validator.cpp
${wasm_HEADERS}
)
# 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)
endif()
add_library(wasm OBJECT ${wasm_SOURCES})
target_sources(binaryen PRIVATE ${wasm_SOURCES})
5 changes: 3 additions & 2 deletions third_party/llvm-project/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -73,4 +73,5 @@ 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})
target_link_libraries(binaryen llvm_dwarf)

0 comments on commit 7074d87

Please sign in to comment.