Skip to content

build: copy static/shared library support from libdispatch #1786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 28, 2018
Merged
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
26 changes: 18 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ project(Foundation
C)
enable_testing()

option(BUILD_SHARED_LIBS "build shared libraries" ON)

option(FOUNDATION_ENABLE_LIBDISPATCH "Enable GCD Support" YES)
option(FOUNDATION_PATH_TO_LIBDISPATCH_SOURCE "Path to libdispatch source" "")
option(FOUNDATION_PATH_TO_LIBDISPATCH_BUILD "Path to libdispatch build" "")
Expand Down Expand Up @@ -259,8 +261,9 @@ add_swift_library(Foundation
-I;${ICU_INCLUDE_DIR}
${libdispatch_cflags}
${swift_enable_testing}
${swift_optimization_flags})
add_dependencies(Foundation CoreFoundation)
${swift_optimization_flags}
DEPENDS
CoreFoundation)

add_swift_executable(plutil
SOURCES
Expand All @@ -280,8 +283,10 @@ add_swift_executable(plutil
-I;${ICU_INCLUDE_DIR}
${libdispatch_cflags}
${swift_enable_testing}
${swift_optimization_flags})
add_dependencies(plutil Foundation CoreFoundation)
${swift_optimization_flags}
DEPENDS
Foundation
CoreFoundation)

if(ENABLE_TESTING)
add_swift_executable(xdgTestHelper
Expand All @@ -298,8 +303,10 @@ if(ENABLE_TESTING)
SWIFT_FLAGS
-I;${CMAKE_CURRENT_BINARY_DIR}/swift
-I;${ICU_INCLUDE_DIR}
${libdispatch_cflags})
add_dependencies(xdgTestHelper Foundation CoreFoundation)
${libdispatch_cflags}
DEPENDS
Foundation
CoreFoundation)

add_swift_executable(TestFoundation
SOURCES
Expand Down Expand Up @@ -429,8 +436,11 @@ if(ENABLE_TESTING)
-I;${FOUNDATION_PATH_TO_XCTEST_BUILD}/swift
-I;${ICU_INCLUDE_DIR}
${libdispatch_cflags}
${swift_optimization_flags})
add_dependencies(TestFoundation Foundation CoreFoundation xdgTestHelper)
${swift_optimization_flags}
DEPENDS
Foundation
CoreFoundation
xdgTestHelper)

add_custom_command(TARGET TestFoundation
POST_BUILD
Expand Down
86 changes: 71 additions & 15 deletions cmake/modules/SwiftSupport.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
include(CMakeParseArguments)

function(add_swift_target target)
set(options LIBRARY)
set(options LIBRARY;SHARED;STATIC)
set(single_value_options MODULE_NAME;MODULE_LINK_NAME;MODULE_PATH;MODULE_CACHE_PATH;OUTPUT;TARGET)
set(multiple_value_options CFLAGS;DEPENDS;LINK_FLAGS;RESOURCES;SOURCES;SWIFT_FLAGS)

Expand Down Expand Up @@ -44,13 +44,35 @@ function(add_swift_target target)
list(APPEND link_flags ${flag})
endforeach()
endif()
if(AST_LIBRARY)
if(AST_STATIC AND AST_SHARED)
message(SEND_ERROR "add_swift_target asked to create library as STATIC and SHARED")
elseif(AST_STATIC OR NOT BUILD_SHARED_LIBS)
set(library_kind STATIC)
elseif(AST_SHARED OR BUILD_SHARED_LIBS)
set(library_kind SHARED)
endif()
else()
if(AST_STATIC OR AST_SHARED)
message(SEND_ERROR "add_swift_target asked to create executable as STATIC or SHARED")
endif()
endif()
if(NOT AST_OUTPUT)
if(AST_LIBRARY)
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${CMAKE_SHARED_LIBRARY_PREFIX}${target}${CMAKE_SHARED_LIBRARY_SUFFIX})
if(AST_SHARED OR BUILD_SHARED_LIBS)
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${CMAKE_SHARED_LIBRARY_PREFIX}${target}${CMAKE_SHARED_LIBRARY_SUFFIX})
else()
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${CMAKE_STATIC_LIBRARY_PREFIX}${target}${CMAKE_STATIC_LIBRARY_SUFFIX})
endif()
else()
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${target}${CMAKE_EXECUTABLE_SUFFIX})
endif()
endif()
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
if(AST_SHARED OR BUILD_SHARED_LIBS)
set(IMPORT_LIBRARY ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${CMAKE_IMPORT_LIBRARY_PREFIX}${target}${CMAKE_IMPORT_LIBRARY_SUFFIX})
endif()
endif()

set(sources)
foreach(source ${AST_SOURCES})
Expand Down Expand Up @@ -113,19 +135,42 @@ function(add_swift_target target)
if(AST_LIBRARY)
set(emit_library -emit-library)
endif()
add_custom_command(OUTPUT
${AST_OUTPUT}
DEPENDS
${objs}
${AST_DEPENDS}
COMMAND
${CMAKE_SWIFT_COMPILER} ${emit_library} ${link_flags} -o ${AST_OUTPUT} ${objs})
add_custom_target(${target}
ALL
DEPENDS
${AST_OUTPUT}
${module}
${documentation})
if(NOT AST_LIBRARY OR library_kind STREQUAL SHARED)
add_custom_command(OUTPUT
${AST_OUTPUT}
DEPENDS
${objs}
${AST_DEPENDS}
COMMAND
${CMAKE_SWIFT_COMPILER} ${emit_library} ${link_flags} -o ${AST_OUTPUT} ${objs})
add_custom_target(${target}
ALL
DEPENDS
${AST_OUTPUT}
${module}
${documentation})
else()
add_library(${target}-static STATIC ${objs})
if(AST_DEPENDS)
add_dependencies(${target}-static ${AST_DEPENDS})
endif()
get_filename_component(ast_output_bn ${AST_OUTPUT} NAME)
string(REGEX REPLACE "^${CMAKE_STATIC_LIBRARY_PREFIX}" "" ast_output_bn ${ast_output_bn})
string(REGEX REPLACE "${CMAKE_STATIC_LIBRARY_SUFFIX}$" "" ast_output_bn ${ast_output_bn})
get_filename_component(ast_output_dn ${AST_OUTPUT} DIRECTORY)
set_target_properties(${target}-static
PROPERTIES
LINKER_LANGUAGE C
ARCHIVE_OUTPUT_DIRECTORY ${ast_output_dn}
OUTPUT_DIRECTORY ${ast_output_dn}
OUTPUT_NAME ${ast_output_bn})
add_custom_target(${target}
ALL
DEPENDS
${target}-static
${module}
${documentation})
endif()

if(AST_RESOURCES)
add_custom_command(TARGET
Expand All @@ -150,6 +195,15 @@ function(add_swift_target target)
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy ${AST_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR})
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
if(AST_SHARED OR BUILD_SHARED_LIBS)
add_custom_command(TARGET
${target}
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy ${IMPORT_LIBRARY} ${CMAKE_CURRENT_BINARY_DIR})
endif()
endif()
endif()
endfunction()

Expand Down Expand Up @@ -189,6 +243,8 @@ function(get_swift_host_arch result_var_name)
set("${result_var_name}" "itanium" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86")
set("${result_var_name}" "i686" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686")
set("${result_var_name}" "i686" PARENT_SCOPE)
else()
message(FATAL_ERROR "Unrecognized architecture on host system: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
Expand Down