authorgravatar for bratishkaerik@getgoogleoff.meEric Joldasov <bratishkaerik@getgoogleoff.me> 2024-05-14 19:31:06+05:00
committergravatar for bratishkaerik@landless-city.netEric Joldasov <bratishkaerik@landless-city.net> 2024-05-14 23:40:41+05:00
log1f7ca6b2dfca66032342c82ec25ae28e6ec02e21
tree218f87e2d8b87bed9576c178d02148b880bca222
parent1ffa6a0e799a67631647d8ad7651fc4c2e56897d
signaturelock-open Commit is signed but in an unrecognized format.

CMake: refactor "zigcpp" target logic

* Localize most of the global properties and functions, for some time they are only needed for "zigcpp" static library (sometimes with PUBLIC keyword, so that it will propagate to zig2): `CMAKE_*_OUTPUT_DIRECTORY` and two calls to `include_directories`. This removes useless flags when building other targets and cleans build log a bit. * Remove `EXE_CXX_FLAGS` variable, instead use more appropriate specific properties and functions for this target. This gives better errors if compiler does not support some of them, and CMake also handles for us duplicate flags. It's also easier to read side-by-side with same flags from build.zig . * Add some comments. Signed-off-by: Eric Joldasov <bratishkaerik@landless-city.net>

1 files changed, 49 insertions(+), 37 deletions(-)

CMakeLists.txt+49-37
...@@ -175,23 +175,6 @@ if(ZIG_STATIC_CURSES)...@@ -175,23 +175,6 @@ if(ZIG_STATIC_CURSES)
175 list(APPEND LLVM_LIBRARIES "${CURSES}")175 list(APPEND LLVM_LIBRARIES "${CURSES}")
176endif()176endif()
177177
178set(ZIG_CPP_LIB_DIR "${CMAKE_BINARY_DIR}/zigcpp")
179
180# Handle multi-config builds and place each into a common lib. The VS generator
181# for example will append a Debug folder by default if not explicitly specified.
182set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${ZIG_CPP_LIB_DIR})
183set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${ZIG_CPP_LIB_DIR})
184foreach(CONFIG_TYPE ${CMAKE_CONFIGURATION_TYPES})
185 string(TOUPPER ${CONFIG_TYPE} CONFIG_TYPE)
186 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_TYPE} ${ZIG_CPP_LIB_DIR})
187 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONFIG_TYPE} ${ZIG_CPP_LIB_DIR})
188 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${CONFIG_TYPE} ${CMAKE_BINARY_DIR})
189endforeach(CONFIG_TYPE CMAKE_CONFIGURATION_TYPES)
190
191include_directories(${LLVM_INCLUDE_DIRS})
192include_directories(${LLD_INCLUDE_DIRS})
193include_directories(${CLANG_INCLUDE_DIRS})
194
195find_package(Threads)178find_package(Threads)
196179
197set(ZIG_CONFIG_H_OUT "${CMAKE_BINARY_DIR}/config.h")180set(ZIG_CONFIG_H_OUT "${CMAKE_BINARY_DIR}/config.h")
...@@ -200,6 +183,7 @@ set(ZIG_CONFIG_ZIG_OUT "${CMAKE_BINARY_DIR}/config.zig")...@@ -200,6 +183,7 @@ set(ZIG_CONFIG_ZIG_OUT "${CMAKE_BINARY_DIR}/config.zig")
200set(ZIG_WASM2C_SOURCES183set(ZIG_WASM2C_SOURCES
201 "${CMAKE_SOURCE_DIR}/stage1/wasm2c.c"184 "${CMAKE_SOURCE_DIR}/stage1/wasm2c.c"
202)185)
186# Sync with "zig_cpp_sources" in build.zig
203set(ZIG_CPP_SOURCES187set(ZIG_CPP_SOURCES
204 # These are planned to stay even when we are self-hosted.188 # These are planned to stay even when we are self-hosted.
205 "${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp"189 "${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp"
...@@ -713,36 +697,64 @@ configure_file (...@@ -713,36 +697,64 @@ configure_file (
713 "${ZIG_CONFIG_ZIG_OUT}"697 "${ZIG_CONFIG_ZIG_OUT}"
714)698)
715699
716include_directories(700# zigcpp target
717 ${CMAKE_SOURCE_DIR}
718 ${CMAKE_BINARY_DIR}
719 "${CMAKE_SOURCE_DIR}/src"
720)
721701
722if(MSVC)702set(ZIGCPP_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/zigcpp")
723 set(EXE_CXX_FLAGS "/std:c++17")
724else()
725 set(EXE_CXX_FLAGS "-std=c++17 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D_GNU_SOURCE -fvisibility-inlines-hidden -fno-exceptions -fno-rtti -Wno-type-limits -Wno-missing-braces -Wno-comment")
726 if(MINGW)
727 set(EXE_CXX_FLAGS "${EXE_CXX_FLAGS} -Wno-format")
728 endif()
729endif()
730703
731add_library(zigcpp STATIC ${ZIG_CPP_SOURCES})704add_library(zigcpp STATIC ${ZIG_CPP_SOURCES})
732if(ZIG_PIE)
733 set(ZIGCPP_CXX_FLAGS "${EXE_CXX_FLAGS} -fno-stack-protector -fPIC")
734else()
735 set(ZIGCPP_CXX_FLAGS "${EXE_CXX_FLAGS} -fno-stack-protector")
736endif()
737set_target_properties(zigcpp PROPERTIES COMPILE_FLAGS ${ZIGCPP_CXX_FLAGS})
738705
739target_link_libraries(zigcpp LINK_PUBLIC706# Sync with minimum C++ standard required to build LLVM
707# and with "exe_cflags" in build.zig
708target_compile_features(zigcpp PRIVATE cxx_std_17)
709set_target_properties(zigcpp PROPERTIES POSITION_INDEPENDENT_CODE ${ZIG_PIE})
710
711if(NOT MSVC)
712 if(MINGW)
713 target_compile_options(zigcpp PRIVATE -Wno-format)
714 endif()
715 # Sync content below with "exe_cflags" in build.zig
716 target_compile_definitions(zigcpp PUBLIC
717 __STDC_CONSTANT_MACROS
718 __STDC_FORMAT_MACROS
719 __STDC_LIMIT_MACROS
720
721 _GNU_SOURCE
722 )
723 target_compile_options(zigcpp PRIVATE
724 -fno-exceptions
725 -fno-rtti
726 -fno-stack-protector
727
728 -fvisibility-inlines-hidden
729
730 -Wno-type-limits
731 -Wno-missing-braces
732 -Wno-comment
733 )
734endif()
735
736target_include_directories(zigcpp PUBLIC
737 ${CLANG_INCLUDE_DIRS}
738 ${LLVM_INCLUDE_DIRS}
739 ${LLD_INCLUDE_DIRS}
740)
741target_link_libraries(zigcpp PUBLIC
740 ${CLANG_LIBRARIES}742 ${CLANG_LIBRARIES}
741 ${LLD_LIBRARIES}743 ${LLD_LIBRARIES}
742 ${LLVM_LIBRARIES}744 ${LLVM_LIBRARIES}
743 ${CMAKE_THREAD_LIBS_INIT}745 ${CMAKE_THREAD_LIBS_INIT}
744)746)
745747
748# Handle multi-config builds and place each into a common lib. The VS generator
749# for example will append a Debug folder by default if not explicitly specified.
750set_target_properties(zigcpp PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ZIGCPP_OUTPUT_DIR})
751foreach(CONFIG_TYPE ${CMAKE_CONFIGURATION_TYPES})
752 string(TOUPPER ${CONFIG_TYPE} CONFIG_TYPE)
753 set_target_properties(zigcpp PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_TYPE} ${ZIGCPP_OUTPUT_DIR})
754endforeach()
755
756# end of zigcpp target
757
746string(TOLOWER "${CMAKE_HOST_SYSTEM_NAME}" ZIG_HOST_TARGET_OS)758string(TOLOWER "${CMAKE_HOST_SYSTEM_NAME}" ZIG_HOST_TARGET_OS)
747if(ZIG_HOST_TARGET_OS STREQUAL "darwin")759if(ZIG_HOST_TARGET_OS STREQUAL "darwin")
748 set(ZIG_HOST_TARGET_OS "macos")760 set(ZIG_HOST_TARGET_OS "macos")