Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ bazel-spirv-tools
bazel-SPIRV-Tools
bazel-testlogs
MODULE.bazel.lock
/install

# Vim
[._]*.s[a-w][a-z]
Expand Down
105 changes: 63 additions & 42 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@

cmake_minimum_required(VERSION 3.22.1)

# master project detection--useful for FetchContent/submodule inclusion
set(MASTER_PROJECT OFF)
set(SUBPROJECT ON)

if (NOT DEFINED PROJECT_NAME)
set(MASTER_PROJECT ON)
set(SUBPROJECT OFF)
endif()

project(spirv-tools)

# Avoid a bug in CMake 3.22.1. By default it will set -std=c++11 for
Expand Down Expand Up @@ -135,46 +144,52 @@ 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(ENABLE_EXCEPTIONS_ON_MSVC "Build SPIRV-TOOLS with c++ exceptions enabled in MSVC" ON)
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)
# Static/Shared splitting

# TODO: do we want to make the other targets support this split?

# Avoid conflict between the dll import library and
# the static library (thanks microsoft)
if(CMAKE_STATIC_LIBRARY_PREFIX STREQUAL "" AND
CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL ".lib")
set(SPIRV_TOOLS_STATIC_LIBNAME "${SPIRV_TOOLS}-static")
else()
set(SPIRV_TOOLS_STATIC_LIBNAME "${SPIRV_TOOLS}")
endif()

# default to shared for master project, static for subproject
# TODO should we do this based on BUILD_SHARED_LIBS instead?
option(SPIRV_TOOLS_BUILD_SHARED "Build ${SPIRV_TOOLS} as a shared library"
${MASTER_PROJECT})
option(SPIRV_TOOLS_BUILD_STATIC "Build ${SPIRV_TOOLS} as a static library"
${SUBPROJECT})

# if neither are on... what are you doing?
if (NOT (SPIRV_TOOLS_BUILD_SHARED OR SPIRV_TOOLS_BUILD_STATIC))
message(FATAL_ERROR "SPIRV_TOOLS_BUILD_SHARED or SPIRV_TOOLS_BUILD_STATIC must be set")
endif()

if (SPIRV_TOOLS_BUILD_STATIC)
set(SPIRV_TOOLS_FULL_VISIBILITY ${SPIRV_TOOLS}-static)
set(SPIRV_TOOLS_LIBRARY_TYPE "STATIC")
else(SPIRV_TOOLS_BUILD_STATIC)
else()
# otherwise, we should respect BUILD_SHARED_LIBS
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)
endif()

if (( BUILD_SHARED_LIBS AND SPIRV_TOOLS_BUILD_SHARED ) OR NOT
SPIRV_TOOLS_BUILD_STATIC)
set(SPIRV_TOOLS_FULL_VISIBILITY ${SPIRV_TOOLS}-shared)
set(SPIRV_TOOLS_LIBRARY_TYPE "SHARED")
elseif(SPIRV_TOOLS_BUILD_STATIC)
set(SPIRV_TOOLS_FULL_VISIBILITY ${SPIRV_TOOLS}-static)
set(SPIRV_TOOLS_LIBRARY_TYPE "STATIC")
else()
# BUILD_SHARED_LIBS is OFF, but no static target--just use shared
set(SPIRV_TOOLS_FULL_VISIBILITY ${SPIRV_TOOLS}-shared)
set(SPIRV_TOOLS_LIBRARY_TYPE "SHARED")
endif()

option(ENABLE_EXCEPTIONS_ON_MSVC "Build SPIRV-TOOLS with c++ exceptions enabled in MSVC" ON)

function(spvtools_default_compile_options TARGET)
target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
Expand Down Expand Up @@ -388,7 +403,11 @@ add_custom_command(
-DSPIRV_LIBRARIES=${SPIRV_LIBRARIES}
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake
DEPENDS "CHANGES" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/SPIRV-Tools.pc.in" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake")
add_custom_command(

set(SPIRV_TOOLS_PKGCONFIG_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc)

if (SPIRV_TOOLS_BUILD_SHARED)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc
COMMAND ${CMAKE_COMMAND}
-DCHANGES_FILE=${CMAKE_CURRENT_SOURCE_DIR}/CHANGES
Expand All @@ -400,9 +419,11 @@ add_custom_command(
-DSPIRV_SHARED_LIBRARIES=${SPIRV_SHARED_LIBRARIES}
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake
DEPENDS "CHANGES" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/SPIRV-Tools-shared.pc.in" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake")
add_custom_target(spirv-tools-pkg-config
ALL
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc)
set(SPIRV_TOOLS_PKGCONFIG_DEPENDS ${SPIRV_TOOLS_PKGCONFIG_DEPENDS} ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc)
endif()

add_custom_target(spirv-tools-pkg-config ALL
DEPENDS ${SPIRV_TOOLS_PKGCONFIG_DEPENDS})

# Install pkg-config file
if (ENABLE_SPIRV_TOOLS_INSTALL)
Expand Down
91 changes: 55 additions & 36 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,9 @@ spvtools_pch(SPIRV_SOURCES pch_source)

# spirv_tools_default_target_options() sets the target options that are common
# for all ${SPIRV_TOOLS} targets.
function(spirv_tools_default_target_options target)
function(spirv_tools_default_target_options target install)
spvtools_default_compile_options(${target})

target_include_directories(${target}
PUBLIC
$<BUILD_INTERFACE:${spirv-tools_SOURCE_DIR}/include>
Expand All @@ -335,50 +336,68 @@ function(spirv_tools_default_target_options target)
PRIVATE ${SPIRV_HEADER_INCLUDE_DIR}
)
set_property(TARGET ${target} PROPERTY FOLDER "SPIRV-Tools libraries")
spvtools_check_symbol_exports(${target})

if (${install})
spvtools_check_symbol_exports(${target})
endif()

add_dependencies(${target} spirv-tools-build-version core_tables 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})
if (SPIRV_TOOLS_USE_MIMALLOC)
target_link_libraries(${SPIRV_TOOLS}-shared PRIVATE mimalloc-static)
# object library--used by the dependent shared/static targets
add_library(${SPIRV_TOOLS}-obj OBJECT ${SPIRV_SOURCES})
spirv_tools_default_target_options(${SPIRV_TOOLS}-obj OFF)

if (SPIRV_TOOLS_BUILD_SHARED)
add_library(${SPIRV_TOOLS}-shared SHARED
$<TARGET_OBJECTS:${SPIRV_TOOLS}-obj>)

set_target_properties(${SPIRV_TOOLS}-shared PROPERTIES OUTPUT_NAME "${SPIRV_TOOLS}")

if (SPIRV_TOOLS_USE_MIMALLOC)
target_link_libraries(${SPIRV_TOOLS}-shared PRIVATE mimalloc-static)
endif()

spirv_tools_default_target_options(${SPIRV_TOOLS}-shared ON)
set_target_properties(${SPIRV_TOOLS}-shared PROPERTIES
CXX_VISIBILITY_PRESET hidden)

# TODO: How should we handle symbol visibility + these compiledefs
# w.r.t the object library?
target_compile_definitions(${SPIRV_TOOLS}-shared
PRIVATE SPIRV_TOOLS_IMPLEMENTATION
PUBLIC SPIRV_TOOLS_SHAREDLIB
)
endif()
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})
if (SPIRV_TOOLS_USE_MIMALLOC AND SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD)
target_link_libraries(${SPIRV_TOOLS}-shared PRIVATE mimalloc-static)
endif()
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()
add_library(${SPIRV_TOOLS}-static STATIC
$<TARGET_OBJECTS:${SPIRV_TOOLS}-obj>)

set(SPIRV_TOOLS_TARGETS ${SPIRV_TOOLS}-static ${SPIRV_TOOLS}-shared)
else()
add_library(${SPIRV_TOOLS} ${SPIRV_TOOLS_LIBRARY_TYPE} ${SPIRV_SOURCES})
if (SPIRV_TOOLS_USE_MIMALLOC)
target_link_libraries(${SPIRV_TOOLS} PRIVATE mimalloc-static)
endif()
spirv_tools_default_target_options(${SPIRV_TOOLS})
set(SPIRV_TOOLS_TARGETS ${SPIRV_TOOLS} ${SPIRV_TOOLS}-shared)
if (SPIRV_TOOLS_USE_MIMALLOC AND SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD)
target_link_libraries(${SPIRV_TOOLS}-shared PRIVATE mimalloc-static)
endif()

spirv_tools_default_target_options(${SPIRV_TOOLS}-static ON)

set_target_properties(${SPIRV_TOOLS}-static PROPERTIES OUTPUT_NAME
"${SPIRV_TOOLS_STATIC_LIBNAME}")
endif()

# make sure all targets are accounted for
# NB: do not include object libraries in export sets!
if (SPIRV_TOOLS_BUILD_STATIC)
set(SPIRV_TOOLS_TARGETS ${SPIRV_TOOLS}-static)
endif()

if (SPIRV_TOOLS_BUILD_SHARED)
set(SPIRV_TOOLS_TARGETS ${SPIRV_TOOLS_TARGETS} ${SPIRV_TOOLS}-shared)
endif()

# Create the "${SPIRV_TOOLS}" target. This should be an alias to
# either the static or shared target depending on BUILD_SHARED_LIBS
add_library(${SPIRV_TOOLS} ALIAS ${SPIRV_TOOLS_FULL_VISIBILITY})

if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
find_library(LIBRT rt)
if(LIBRT)
Expand Down