Skip to content

Commit

Permalink
[Build] [refactor] Use keywords instead of plain target_link_librarie…
Browse files Browse the repository at this point in the history
…s CMake (taichi-dev#4864)

* Move LLVM Cmake to its own dir

* Suppress warning from submodules

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Use current source dir

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Separate Vulkan runtime files from codegen

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Use keywords instead of plain target_link_libraries

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
qiao-bo and pre-commit-ci[bot] authored Apr 29, 2022
1 parent 8a3a2d7 commit c2a44d8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 27 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# The Taichi Programming Language
#*********************************************************************

cmake_minimum_required(VERSION 3.12)
cmake_minimum_required(VERSION 3.13)

project(taichi)

Expand Down
60 changes: 38 additions & 22 deletions cmake/TaichiCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,15 @@ add_library(${CORE_LIBRARY_NAME} OBJECT ${TAICHI_CORE_SOURCE})

if (APPLE)
# Ask OS X to minic Linux dynamic linking behavior
target_link_libraries(${CORE_LIBRARY_NAME} "-undefined dynamic_lookup")
set_target_properties(${CORE_LIBRARY_NAME}
PROPERTIES INTERFACE_LINK_LIBRARIES "-undefined dynamic_lookup"
)
endif()

include_directories(${CMAKE_SOURCE_DIR})
include_directories(external/include)
include_directories(external/spdlog/include)
include_directories(external/SPIRV-Tools/include)
include_directories(external/PicoSHA2)
if (TI_WITH_OPENGL)
target_include_directories(${CORE_LIBRARY_NAME} PRIVATE external/glad/include)
Expand All @@ -272,7 +275,7 @@ if (TI_WITH_OPENGL OR TI_WITH_VULKAN AND NOT ANDROID AND NOT TI_EMSCRIPTENED)

message("Building with GLFW")
add_subdirectory(external/glfw)
target_link_libraries(${LIBRARY_NAME} glfw)
target_link_libraries(${LIBRARY_NAME} PRIVATE glfw)
target_include_directories(${CORE_LIBRARY_NAME} PRIVATE external/glfw/include)
endif()

Expand Down Expand Up @@ -314,16 +317,16 @@ if(TI_WITH_LLVM)
ipo
Analysis
)
target_link_libraries(${LIBRARY_NAME} ${llvm_libs})
target_link_libraries(${LIBRARY_NAME} PRIVATE ${llvm_libs})

if (APPLE AND "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64")
llvm_map_components_to_libnames(llvm_aarch64_libs AArch64)
target_link_libraries(${LIBRARY_NAME} ${llvm_aarch64_libs})
target_link_libraries(${LIBRARY_NAME} PRIVATE ${llvm_aarch64_libs})
endif()

if (TI_WITH_CUDA)
llvm_map_components_to_libnames(llvm_ptx_libs NVPTX)
target_link_libraries(${LIBRARY_NAME} ${llvm_ptx_libs})
target_link_libraries(${LIBRARY_NAME} PRIVATE ${llvm_ptx_libs})
endif()
endif()

Expand All @@ -337,7 +340,7 @@ if (TI_WITH_CUDA_TOOLKIT)
include_directories($ENV{CUDA_TOOLKIT_ROOT_DIR}/include)
link_directories($ENV{CUDA_TOOLKIT_ROOT_DIR}/lib64)
#libraries for cuda kernel profiler CuptiToolkit
target_link_libraries(${CORE_LIBRARY_NAME} cupti nvperf_host)
target_link_libraries(${CORE_LIBRARY_NAME} PRIVATE cupti nvperf_host)
endif()
else()
message(STATUS "TI_WITH_CUDA_TOOLKIT = OFF")
Expand All @@ -347,13 +350,13 @@ if (TI_WITH_OPENGL)
set(SPIRV_CROSS_CLI false)
add_subdirectory(external/SPIRV-Cross)
target_include_directories(${CORE_LIBRARY_NAME} PRIVATE external/SPIRV-Cross)
target_link_libraries(${CORE_LIBRARY_NAME} spirv-cross-glsl spirv-cross-core)
target_link_libraries(${CORE_LIBRARY_NAME} PRIVATE spirv-cross-glsl spirv-cross-core)
endif()

if (TI_WITH_DX11)
set(SPIRV_CROSS_CLI false)
#target_include_directories(${CORE_LIBRARY_NAME} PRIVATE external/SPIRV-Cross)
target_link_libraries(${CORE_LIBRARY_NAME} spirv-cross-hlsl spirv-cross-core)
target_link_libraries(${CORE_LIBRARY_NAME} PRIVATE spirv-cross-hlsl spirv-cross-core)
endif()

# SPIR-V codegen is always there, regardless of Vulkan
Expand All @@ -362,7 +365,7 @@ set(SPIRV-Headers_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/SPIRV-Headers)
add_subdirectory(external/SPIRV-Tools)
# NOTE: SPIRV-Tools-opt must come before SPIRV-Tools
# https://github.com/KhronosGroup/SPIRV-Tools/issues/1569#issuecomment-390250792
target_link_libraries(${CORE_LIBRARY_NAME} SPIRV-Tools-opt ${SPIRV_TOOLS})
target_link_libraries(${CORE_LIBRARY_NAME} PRIVATE SPIRV-Tools-opt ${SPIRV_TOOLS})

if (TI_WITH_VULKAN)
include_directories(SYSTEM external/Vulkan-Headers/include)
Expand All @@ -377,7 +380,7 @@ if (TI_WITH_VULKAN)
# shaderc requires pthread
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(${CORE_LIBRARY_NAME} Threads::Threads)
target_link_libraries(${CORE_LIBRARY_NAME} PRIVATE Threads::Threads)
endif()

if (APPLE)
Expand All @@ -390,44 +393,57 @@ if (TI_WITH_VULKAN)
endif()

add_subdirectory(taichi/runtime/vulkan)
target_link_libraries(${CORE_LIBRARY_NAME} vulkan_runtime)
target_link_libraries(${CORE_LIBRARY_NAME} PRIVATE vulkan_runtime)
endif ()


# Optional dependencies

if (APPLE)
target_link_libraries(${CORE_LIBRARY_NAME} "-framework Cocoa -framework Metal")
find_library(COCOA Cocoa)
if (NOT COCOA)
message(FATAL_ERROR "Cocoa not found")
endif()
find_library(METAL Metal)
if (NOT METAL)
message(FATAL_ERROR "Metal not found")
endif()
target_link_libraries(${CORE_LIBRARY_NAME}
PRIVATE
${COCOA}
${METAL}
)
endif ()

if (NOT WIN32)
# Android has a custom toolchain so pthread is not available and should
# link against other libraries as well for logcat and internal features.
if (ANDROID)
target_link_libraries(${CORE_LIBRARY_NAME} android log)
target_link_libraries(${CORE_LIBRARY_NAME} PRIVATE android log)
else()
target_link_libraries(${CORE_LIBRARY_NAME} pthread stdc++)
target_link_libraries(${CORE_LIBRARY_NAME} PRIVATE pthread stdc++)
endif()

if (UNIX AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Linux")
# OS X or BSD
else()
# Linux
target_link_libraries(${CORE_LIBRARY_NAME} stdc++fs X11)
target_link_libraries(${CORE_LIBRARY_NAME} -static-libgcc -static-libstdc++)
target_link_libraries(${CORE_LIBRARY_NAME} PRIVATE stdc++fs X11)

target_link_options(${CORE_LIBRARY_NAME} PRIVATE -static-libgcc -static-libstdc++)
if (${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "x86_64")
# Avoid glibc dependencies
if (TI_WITH_VULKAN)
target_link_libraries(${CORE_LIBRARY_NAME} -Wl,--wrap=log2f)
target_link_options(${CORE_LIBRARY_NAME} PRIVATE -Wl,--wrap=log2f)
else()
# Enforce compatibility with manylinux2014
target_link_libraries(${CORE_LIBRARY_NAME} -Wl,--wrap=log2f -Wl,--wrap=exp2 -Wl,--wrap=log2 -Wl,--wrap=logf -Wl,--wrap=powf -Wl,--wrap=exp -Wl,--wrap=log -Wl,--wrap=pow)
target_link_options(${CORE_LIBRARY_NAME} PRIVATE -Wl,--wrap=log2f -Wl,--wrap=exp2 -Wl,--wrap=log2 -Wl,--wrap=logf -Wl,--wrap=powf -Wl,--wrap=exp -Wl,--wrap=log -Wl,--wrap=pow)
endif()
endif()
endif()
else()
# windows
target_link_libraries(${CORE_LIBRARY_NAME} Winmm)
target_link_libraries(${CORE_LIBRARY_NAME} PRIVATE Winmm)
endif ()

foreach (source IN LISTS TAICHI_CORE_SOURCE)
Expand Down Expand Up @@ -455,7 +471,7 @@ if(NOT TI_EMSCRIPTENED)
endif()
# It is actually possible to link with an OBJECT library
# https://cmake.org/cmake/help/v3.13/command/target_link_libraries.html?highlight=target_link_libraries#linking-object-libraries
target_link_libraries(${CORE_WITH_PYBIND_LIBRARY_NAME} PUBLIC ${CORE_LIBRARY_NAME})
target_link_libraries(${CORE_WITH_PYBIND_LIBRARY_NAME} PRIVATE ${CORE_LIBRARY_NAME})

# These commands should apply to the DLL that is loaded from python, not the OBJECT library.
if (MSVC)
Expand All @@ -475,7 +491,7 @@ endif()
if(TI_EMSCRIPTENED)
set(CORE_WITH_EMBIND_LIBRARY_NAME taichi)
add_executable(${CORE_WITH_EMBIND_LIBRARY_NAME} ${TAICHI_EMBIND_SOURCE})
target_link_libraries(${CORE_WITH_EMBIND_LIBRARY_NAME} PUBLIC ${CORE_LIBRARY_NAME})
target_link_libraries(${CORE_WITH_EMBIND_LIBRARY_NAME} PRIVATE ${CORE_LIBRARY_NAME})
target_compile_options(${CORE_WITH_EMBIND_LIBRARY_NAME} PRIVATE "-Oz")
# target_compile_options(${CORE_LIBRARY_NAME} PRIVATE "-Oz")
set_target_properties(${CORE_LIBRARY_NAME} PROPERTIES LINK_FLAGS "-s ERROR_ON_UNDEFINED_SYMBOLS=0 -s ASSERTIONS=1")
Expand All @@ -495,7 +511,7 @@ else()
include_directories(external/glfw/include)
add_library(imgui ${IMGUI_DIR}/backends/imgui_impl_glfw.cpp ${IMGUI_DIR}/backends/imgui_impl_vulkan.cpp ${IMGUI_DIR}/imgui.cpp ${IMGUI_DIR}/imgui_draw.cpp ${IMGUI_DIR}/imgui_tables.cpp ${IMGUI_DIR}/imgui_widgets.cpp)
endif()
target_link_libraries(${CORE_LIBRARY_NAME} imgui)
target_link_libraries(${CORE_LIBRARY_NAME} PRIVATE imgui)

endif()

Expand Down
2 changes: 1 addition & 1 deletion cmake/TaichiExamples.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ if (WIN32)
set_target_properties(${EXAMPLES_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${EXAMPLES_OUTPUT_DIR})
set_target_properties(${EXAMPLES_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${EXAMPLES_OUTPUT_DIR})
endif()
target_link_libraries(${EXAMPLES_NAME} taichi_isolated_core)
target_link_libraries(${EXAMPLES_NAME} PRIVATE taichi_isolated_core)

endif()
2 changes: 1 addition & 1 deletion cmake/TaichiExportCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ cmake_minimum_required(VERSION 3.0)
set(TAICHI_EXPORT_CORE_NAME taichi_export_core)

add_library(${TAICHI_EXPORT_CORE_NAME} SHARED)
target_link_libraries(${TAICHI_EXPORT_CORE_NAME} taichi_isolated_core)
target_link_libraries(${TAICHI_EXPORT_CORE_NAME} PRIVATE taichi_isolated_core)
set_target_properties(${TAICHI_EXPORT_CORE_NAME} PROPERTIES
CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/build")
4 changes: 2 additions & 2 deletions cmake/TaichiTests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if (WIN32)
set_target_properties(${TESTS_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${TESTS_OUTPUT_DIR})
set_target_properties(${TESTS_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${TESTS_OUTPUT_DIR})
endif()
target_link_libraries(${TESTS_NAME} taichi_isolated_core)
target_link_libraries(${TESTS_NAME} gtest_main)
target_link_libraries(${TESTS_NAME} PRIVATE taichi_isolated_core)
target_link_libraries(${TESTS_NAME} PRIVATE gtest_main)

add_test(NAME ${TESTS_NAME} COMMAND ${TESTS_NAME})

0 comments on commit c2a44d8

Please sign in to comment.