Skip to content

Commit

Permalink
Attemp to fix KhronosGroup#3909
Browse files Browse the repository at this point in the history
- Try to fix the point (1).
  - Build static unconditionally. but is only used for link all `spirv-foo` programs. because all of them (or partial) need all symbols which is only provided by static libs. for install it. see below
    This can increase the size a bit.
  - Now exist two defines witch control what type of library is installed/builded*:
     - `SPIRV_TOOLS_BUILD_SHARED_LIBS`   -> this control if you want build shared libs. and if is builded, install it when invoke `make install`
     - `SPIRV_TOOLS_INSTALL_STATIC_LIBS` -> this not control the build static library (because is ON unconditionality), but can control the installation when invoke `make install`
     -- I do this because some distros needs only static libs, other only shared libs, other both, or other simply what install only the programs
     --- Table of individual use:

        `SPIRV_TOOLS_BUILD_SHARED_LIBS` not set   -> Disable build and installation of shared libraries
        `SPIRV_TOOLS_BUILD_SHARED_LIBS=ON'        -> Enable build and install shared libs
        `SPIRV_TOOLS_INSTALL_STATIC_LIBS` not set -> Enable by default. enable the installation the libraries when invoke `make install`
        `SPIRV_TOOLS_INSTALL_STATIC_LIBS=ON`      -> Same as not set

    --- Table of conjuntion use:

        `SPIRV_TOOLS_BUILD_SHARED_LIBS=ON'  and `SPIRV_TOOLS_INSTALL_STATIC_LIBS=ON`  -> Build and install the wrole project. include programs, static and shared libs
        `SPIRV_TOOLS_BUILD_SHARED_LIBS=OFF' and `SPIRV_TOOLS_INSTALL_STATIC_LIBS=ON`  -> Build the programs and static libs, and install the static libs and the programs when invoke `make install` (Default)
        `SPIRV_TOOLS_BUILD_SHARED_LIBS=ON'  and `SPIRV_TOOLS_INSTALL_STATIC_LIBS=OFF` -> build the programs, shared and static libs, and only install the programs and the shared libs when invoke `make install`
        `SPIRV_TOOLS_BUILD_SHARED_LIBS=OFF' and `SPIRV_TOOLS_INSTALL_STATIC_LIBS=OFF` -> build the programs and the static libs, but not install when invoke `make install`

       * In all cases, always install the headers

  - The generation of the `.cmake` files is changed. now generate two flavors: one for static (without any sufix), and other for shared (whit `shared` sufix). both can install with the two defines described above. (and not colided between both)
    -- This need HARD test. this need a CMakeLists.txt test case wich can get the info when use static libs or use shared libs. and test if can linked with other things.
       I have desing this for can coinstall shared and static in same time, an let the user what need in your project. cmake can search one or other with only need add `STATIC` or `SHARED` in the `findpackage(FOO >TYPE<)`.
  - The files `libSPIRV-Tools-shared.so` and `SPIRV-Tools-shared.pc` is completely gone. no need anymore. is a duplicate lib when build shared libs, and useless whe build static libs (who ise a shared lib when the dev use static libs?)
  - By design, the common `SPIRV-Tools.pc` can be used in both build modes. the `pkg-config` stack can handle the information of static libs with `--static` flag when exist static libs in the library path, and normal use when is used as shared lib.
    Maybe need ajust for include this information (if exist in the project) into `SPIRV-Tools.pc.in` and setup in the `CMakeFiles.txt`.
  - The visiblility of the shared/static libs is untouched: Hiden for `libSPIRV-Tools.so` and full visible for static.. the other libs originally have ones hiden and other full visible. i leave it untouch. but need test if is all ok.

TODO:
  - Fix point (2) and point (3)

- Point (4) is out of my scope. i'm not coder
  • Loading branch information
sl1pkn07 committed Oct 16, 2021
1 parent 35fd0e1 commit 2a4b11e
Show file tree
Hide file tree
Showing 10 changed files with 401 additions and 242 deletions.
80 changes: 21 additions & 59 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,45 +139,10 @@ if (DEFINED SPIRV_TOOLS_EXTRA_DEFINITIONS)
add_definitions(${SPIRV_TOOLS_EXTRA_DEFINITIONS})
endif()

# Library build setting definitions:
#
# * SPIRV_TOOLS_BUILD_STATIC - ON or OFF - Defaults to ON.
# If enabled the following targets will be created:
# ${SPIRV_TOOLS}-static - STATIC library.
# Has full public symbol visibility.
# ${SPIRV_TOOLS}-shared - SHARED library.
# Has default-hidden symbol visibility.
# ${SPIRV_TOOLS} - will alias to one of above, based on BUILD_SHARED_LIBS.
# If disabled the following targets will be created:
# ${SPIRV_TOOLS} - either STATIC or SHARED based on SPIRV_TOOLS_LIBRARY_TYPE.
# Has full public symbol visibility.
# ${SPIRV_TOOLS}-shared - SHARED library.
# Has default-hidden symbol visibility.
#
# * SPIRV_TOOLS_LIBRARY_TYPE - SHARED or STATIC.
# Specifies the library type used for building SPIRV-Tools libraries.
# Defaults to SHARED when BUILD_SHARED_LIBS=1, otherwise STATIC.
#
# * SPIRV_TOOLS_FULL_VISIBILITY - "${SPIRV_TOOLS}-static" or "${SPIRV_TOOLS}"
# Evaluates to the SPIRV_TOOLS target library name that has no hidden symbols.
# This is used by internal targets for accessing symbols that are non-public.
# Note this target provides no API stability guarantees.
#
# Ideally, all of these will go away - see https://github.com/KhronosGroup/SPIRV-Tools/issues/3909.
option(SPIRV_TOOLS_BUILD_STATIC "Build ${SPIRV_TOOLS}-static target. ${SPIRV_TOOLS} will alias to ${SPIRV_TOOLS}-static or ${SPIRV_TOOLS}-shared based on BUILD_SHARED_LIBS" ON)
if(SPIRV_TOOLS_BUILD_STATIC)
set(SPIRV_TOOLS_FULL_VISIBILITY ${SPIRV_TOOLS}-static)
set(SPIRV_TOOLS_LIBRARY_TYPE "STATIC")
else(SPIRV_TOOLS_BUILD_STATIC)
set(SPIRV_TOOLS_FULL_VISIBILITY ${SPIRV_TOOLS})
if (NOT DEFINED SPIRV_TOOLS_LIBRARY_TYPE)
if(BUILD_SHARED_LIBS)
set(SPIRV_TOOLS_LIBRARY_TYPE "SHARED")
else()
set(SPIRV_TOOLS_LIBRARY_TYPE "STATIC")
endif()
endif()
endif(SPIRV_TOOLS_BUILD_STATIC)
option(SPIRV_TOOLS_INSTALL_STATIC_LIBS "Enable static libs" ON)
option(SPIRV_TOOLS_BUILD_SHARED_LIBS "Enable shared libs" OFF)

set(SPIRV_TOOLS_FULL_VISIBILITY ${SPIRV_TOOLS})

function(spvtools_default_compile_options TARGET)
target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
Expand Down Expand Up @@ -276,9 +241,17 @@ if(ENABLE_SPIRV_TOOLS_INSTALL)
file(WRITE ${CMAKE_BINARY_DIR}/${TARGET}Config.cmake
"include(CMakeFindDependencyMacro)\n"
"find_dependency(${SPIRV_TOOLS})\n"
"include(\${CMAKE_CURRENT_LIST_DIR}/${TARGET}Targets.cmake)\n"
"get_target_property(target_type ${TARGET} TYPE)\n"
"if (target_type STREQUAL STATIC_LIBRARY)\n"
"include(\${CMAKE_CURRENT_LIST_DIR}/${TARGET}-staticTargets.cmake)\n"
"set(${TARGET}_LIBRARIES ${TARGET})\n"
"get_target_property(${TARGET}_INCLUDE_DIRS ${TARGET} INTERFACE_INCLUDE_DIRECTORIES)\n")
"get_target_property(${TARGET}_INCLUDE_DIRS ${TARGET} INTERFACE_INCLUDE_DIRECTORIES)\n"
"endif()\n"
"if (target_type STREQUAL SHARED_LIBRARY)\n"
"include(\${CMAKE_CURRENT_LIST_DIR}/${TARGET}-sharedTargets.cmake)\n"
"set(${TARGET}_LIBRARIES ${TARGET}-shared)\n"
"get_target_property(${TARGET}_INCLUDE_DIRS ${TARGET}-shared INTERFACE_INCLUDE_DIRECTORIES)\n"
"endif()\n")
endmacro()
endif()

Expand Down Expand Up @@ -353,7 +326,6 @@ if (NOT "${SPIRV_SKIP_TESTS}")
endif()

set(SPIRV_LIBRARIES "-lSPIRV-Tools-opt -lSPIRV-Tools -lSPIRV-Tools-link")
set(SPIRV_SHARED_LIBRARIES "-lSPIRV-Tools-shared")

# Build pkg-config file
# Use a first-class target so it's regenerated when relevant files are updated.
Expand All @@ -368,24 +340,14 @@ add_custom_target(spirv-tools-pkg-config ALL
-DSPIRV_LIBRARIES=${SPIRV_LIBRARIES}
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake
DEPENDS "CHANGES" "cmake/SPIRV-Tools.pc.in" "cmake/write_pkg_config.cmake")
add_custom_target(spirv-tools-shared-pkg-config ALL
COMMAND ${CMAKE_COMMAND}
-DCHANGES_FILE=${CMAKE_CURRENT_SOURCE_DIR}/CHANGES
-DTEMPLATE_FILE=${CMAKE_CURRENT_SOURCE_DIR}/cmake/SPIRV-Tools-shared.pc.in
-DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
-DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR}
-DCMAKE_INSTALL_INCLUDEDIR=${CMAKE_INSTALL_INCLUDEDIR}
-DSPIRV_SHARED_LIBRARIES=${SPIRV_SHARED_LIBRARIES}
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake
DEPENDS "CHANGES" "cmake/SPIRV-Tools-shared.pc.in" "cmake/write_pkg_config.cmake")

# Install pkg-config file
if (ENABLE_SPIRV_TOOLS_INSTALL)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc
${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc
DESTINATION
${CMAKE_INSTALL_LIBDIR}/pkgconfig)
if(SPIRV_TOOLS_BUILD_SHARED_LIBS OR SPIRV_TOOLS_INSTALL_STATIC_LIBS)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc
DESTINATION
${CMAKE_INSTALL_LIBDIR}/pkgconfig)
endif()
endif()
12 changes: 0 additions & 12 deletions cmake/SPIRV-Tools-shared.pc.in

This file was deleted.

13 changes: 10 additions & 3 deletions external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ if (NOT ${SPIRV_SKIP_TESTS})
"Use shared (DLL) run-time lib even when Google Test is built as static lib."
ON)
endif()
# gtest requires special defines for building as a shared
# Gtest requires special defines for building as a shared
# library, simply always build as static.
push_variable(BUILD_SHARED_LIBS 0)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Disable build shared libs")
add_subdirectory(${GMOCK_DIR} EXCLUDE_FROM_ALL)
pop_variable(BUILD_SHARED_LIBS)
endif()
Expand All @@ -102,7 +103,10 @@ if (NOT ${SPIRV_SKIP_TESTS})
# If we are configuring RE2, then turn off its testing. It takes a long time and
# does not add much value for us. If an enclosing project configured RE2, then it
# has already chosen whether to enable RE2 testing.
set(RE2_BUILD_TESTING OFF CACHE STRING "Run RE2 Tests")
set(RE2_BUILD_TESTING OFF CACHE BOOL "Run RE2 Tests")
# Build static RE2. do less problems if run the test when you have already RE2
# in the system and have different library version.
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Disable build shared libs")
if (NOT RE2_SOURCE_DIR)
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/re2)
set(RE2_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/re2" CACHE STRING "RE2 source dir" )
Expand All @@ -125,7 +129,10 @@ if (NOT ${SPIRV_SKIP_TESTS})
if (NOT TARGET effcee)
set(EFFCEE_BUILD_TESTING OFF CACHE BOOL "Do not build Effcee test suite")
endif()
push_variable(BUILD_SHARED_LIBS 0) # effcee does not export any symbols for building as a DLL. Always build as static.
# Effcee does not export any symbols for building as a DLL. Always build as static.
push_variable(BUILD_SHARED_LIBS 0)
# Expands this if include RE2 in the effcee third_party folder
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Disable build shared libs")
add_subdirectory(effcee EXCLUDE_FROM_ALL)
pop_variable(BUILD_SHARED_LIBS)
set_property(TARGET effcee PROPERTY FOLDER Effcee)
Expand Down
123 changes: 74 additions & 49 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ set_source_files_properties(
${CMAKE_CURRENT_SOURCE_DIR}/software_version.cpp
PROPERTIES OBJECT_DEPENDS "${SPIRV_TOOLS_BUILD_VERSION_INC}")

if(MSVC AND (NOT ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")))
# Enable parallel builds across four cores for this lib
add_definitions(/MP4)
endif()

spvtools_pch(SPIRV_SOURCES pch_source)

# spirv_tools_default_target_options() sets the target options that are common
Expand All @@ -369,37 +374,26 @@ function(spirv_tools_default_target_options target)
add_dependencies(${target} spirv-tools-build-version core_tables enum_string_mapping extinst_tables)
endfunction()

# Always build ${SPIRV_TOOLS}-shared. This is expected distro packages, and
# unlike the other SPIRV_TOOLS target, defaults to hidden symbol visibility.
add_library(${SPIRV_TOOLS}-shared SHARED ${SPIRV_SOURCES})
spirv_tools_default_target_options(${SPIRV_TOOLS}-shared)
set_target_properties(${SPIRV_TOOLS}-shared PROPERTIES CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(${SPIRV_TOOLS}-shared
PRIVATE SPIRV_TOOLS_IMPLEMENTATION
PUBLIC SPIRV_TOOLS_SHAREDLIB
)

if(SPIRV_TOOLS_BUILD_STATIC)
add_library(${SPIRV_TOOLS}-static STATIC ${SPIRV_SOURCES})
spirv_tools_default_target_options(${SPIRV_TOOLS}-static)
# The static target does not have the '-static' suffix.
set_target_properties(${SPIRV_TOOLS}-static PROPERTIES OUTPUT_NAME "${SPIRV_TOOLS}")

# Create the "${SPIRV_TOOLS}" target as an alias to either "${SPIRV_TOOLS}-static"
# or "${SPIRV_TOOLS}-shared" depending on the value of BUILD_SHARED_LIBS.
if(BUILD_SHARED_LIBS)
add_library(${SPIRV_TOOLS} ALIAS ${SPIRV_TOOLS}-shared)
else()
add_library(${SPIRV_TOOLS} ALIAS ${SPIRV_TOOLS}-static)
endif()

set(SPIRV_TOOLS_TARGETS ${SPIRV_TOOLS}-static ${SPIRV_TOOLS}-shared)
else()
add_library(${SPIRV_TOOLS} ${SPIRV_TOOLS_LIBRARY_TYPE} ${SPIRV_SOURCES})
spirv_tools_default_target_options(${SPIRV_TOOLS})
set(SPIRV_TOOLS_TARGETS ${SPIRV_TOOLS} ${SPIRV_TOOLS}-shared)
add_library(${SPIRV_TOOLS} STATIC ${SPIRV_SOURCES})
if(SPIRV_TOOLS_BUILD_SHARED_LIBS)
add_library(${SPIRV_TOOLS}-shared SHARED ${SPIRV_SOURCES})
set_target_properties(${SPIRV_TOOLS}-shared PROPERTIES CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(${SPIRV_TOOLS}-shared
PRIVATE SPIRV_TOOLS_IMPLEMENTATION
PUBLIC SPIRV_TOOLS_SHAREDLIB
)
set(SPIRV_TOOLS_SHARED ${SPIRV_TOOLS}-shared)
endif()

set(SPIRV_TOOLS_DEFAUL_TARGET_OPTIONS ${SPIRV_TOOLS} ${SPIRV_TOOLS_SHARED})

foreach(target ${SPIRV_TOOLS_DEFAUL_TARGET_OPTIONS})
spirv_tools_default_target_options(${target})
endforeach()

set(SPIRV_TOOLS_TARGETS ${SPIRV_TOOLS} ${SPIRV_TOOLS_SHARED})
set(SPIRV_TOOLS_FULL_VISIBILITY ${SPIRV_TOOLS})

if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
find_library(LIBRT rt)
if(LIBRT)
Expand All @@ -416,24 +410,55 @@ if (ANDROID)
endif()

if(ENABLE_SPIRV_TOOLS_INSTALL)
install(TARGETS ${SPIRV_TOOLS_TARGETS} EXPORT ${SPIRV_TOOLS}Targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
export(EXPORT ${SPIRV_TOOLS}Targets FILE ${SPIRV_TOOLS}Target.cmake)

spvtools_config_package_dir(${SPIRV_TOOLS} PACKAGE_DIR)
install(EXPORT ${SPIRV_TOOLS}Targets FILE ${SPIRV_TOOLS}Target.cmake DESTINATION ${PACKAGE_DIR})

# Special config file for root library compared to other libs.
file(WRITE ${CMAKE_BINARY_DIR}/${SPIRV_TOOLS}Config.cmake
"include(\${CMAKE_CURRENT_LIST_DIR}/${SPIRV_TOOLS}Target.cmake)\n"
"set(${SPIRV_TOOLS}_LIBRARIES ${SPIRV_TOOLS})\n"
"get_target_property(${SPIRV_TOOLS}_INCLUDE_DIRS ${SPIRV_TOOLS} INTERFACE_INCLUDE_DIRECTORIES)\n")
install(FILES ${CMAKE_BINARY_DIR}/${SPIRV_TOOLS}Config.cmake DESTINATION ${PACKAGE_DIR})
endif(ENABLE_SPIRV_TOOLS_INSTALL)
if(SPIRV_TOOLS_INSTALL_STATIC_LIBS)
install(TARGETS ${SPIRV_TOOLS} EXPORT ${SPIRV_TOOLS}-Targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
export(EXPORT ${SPIRV_TOOLS}-Targets FILE ${SPIRV_TOOLS}Targets.cmake)

spvtools_config_package_dir(${SPIRV_TOOLS} PACKAGE_DIR)
install(EXPORT ${SPIRV_TOOLS}-Targets FILE ${SPIRV_TOOLS}Targets.cmake DESTINATION ${PACKAGE_DIR})

# Special config file for root library compared to other libs.
file(WRITE ${CMAKE_BINARY_DIR}/${SPIRV_TOOLS}Config.cmake
"get_target_property(target_type \${TARGET} TYPE)\n"
"if (target_type STREQUAL STATIC_LIBRARY)\n"
"include(\${CMAKE_CURRENT_LIST_DIR}/${SPIRV_TOOLS}Targets.cmake)\n"
"get_target_property(${SPIRV_TOOLS}_INCLUDE_DIRS ${SPIRV_TOOLS} INTERFACE_INCLUDE_DIRECTORIES)\n"
"endif()\n"
"if (target_type STREQUAL SHARED_LIBRARY)\n"
"include(\${CMAKE_CURRENT_LIST_DIR}/${SPIRV_TOOLS}-sharedTargets.cmake)\n"
"set(${SPIRV_TOOLS}_LIBRARIES ${SPIRV_TOOLS}-shared)\n"
"get_target_property(${SPIRV_TOOLS}_INCLUDE_DIRS ${SPIRV_TOOLS}-shared INTERFACE_INCLUDE_DIRECTORIES)\n"
"endif()\n")
install(FILES ${CMAKE_BINARY_DIR}/${SPIRV_TOOLS}Config.cmake DESTINATION ${PACKAGE_DIR})
endif()

if(MSVC AND (NOT ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")))
# Enable parallel builds across four cores for this lib
add_definitions(/MP4)
endif()
if(SPIRV_TOOLS_BUILD_SHARED_LIBS)
set_target_properties(${SPIRV_TOOLS}-shared PROPERTIES OUTPUT_NAME ${SPIRV_TOOLS})
set_target_properties(${SPIRV_TOOLS}-shared PROPERTIES EXPORT_NAME ${SPIRV_TOOLS})
install(TARGETS ${SPIRV_TOOLS}-shared EXPORT ${SPIRV_TOOLS}-Targets-shared
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
export(EXPORT ${SPIRV_TOOLS}-Targets-shared FILE ${SPIRV_TOOLS}-sharedTargets.cmake)

spvtools_config_package_dir(${SPIRV_TOOLS} PACKAGE_DIR)
install(EXPORT ${SPIRV_TOOLS}-Targets-shared FILE ${SPIRV_TOOLS}-sharedTargets.cmake DESTINATION ${PACKAGE_DIR})

# Special config file for root library compared to other libs.
file(WRITE ${CMAKE_BINARY_DIR}/${SPIRV_TOOLS}Config.cmake
"get_target_property(target_type ${SPIRV_TOOLS} TYPE)\n"
"if (target_type STREQUAL STATIC_LIBRARY)\n"
"include(\${CMAKE_CURRENT_LIST_DIR}/${SPIRV_TOOLS}Targets.cmake)\n"
"get_target_property(${SPIRV_TOOLS}_INCLUDE_DIRS ${SPIRV_TOOLS} INTERFACE_INCLUDE_DIRECTORIES)\n"
"endif()\n"
"if (target_type STREQUAL SHARED_LIBRARY)\n"
"include(\${CMAKE_CURRENT_LIST_DIR}/${SPIRV_TOOLS}-sharedTargets.cmake)\n"
"set(${SPIRV_TOOLS}_LIBRARIES ${SPIRV_TOOLS}-shared)\n"
"get_target_property(${SPIRV_TOOLS}_INCLUDE_DIRS ${SPIRV_TOOLS}-shared INTERFACE_INCLUDE_DIRECTORIES)\n"
"endif()\n")
install(FILES ${CMAKE_BINARY_DIR}/${SPIRV_TOOLS}Config.cmake DESTINATION ${PACKAGE_DIR})
endif()
endif(ENABLE_SPIRV_TOOLS_INSTALL)
Loading

0 comments on commit 2a4b11e

Please sign in to comment.