Skip to content

Commit

Permalink
Merge pull request #7 from apple/cmake
Browse files Browse the repository at this point in the history
Setup proper CMake for TSC
  • Loading branch information
aciidgh authored Oct 11, 2019
2 parents cabfb9b + 89ddcea commit 54a0b60
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/*.xcodeproj
xcuserdata/
.swiftpm
build
20 changes: 20 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.15.1)

project(SwiftTSC LANGUAGES C Swift)

set(SWIFT_VERSION 5)
set(CMAKE_Swift_LANGUAGE_VERSION ${SWIFT_VERSION})
if(CMAKE_VERSION VERSION_LESS 3.16)
add_compile_options($<$<COMPILE_LANGUAGE:Swift>:-swift-version$<SEMICOLON>${SWIFT_VERSION}>)
endif()

set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

option(BUILD_SHARED_LIBS "Build shared libraryes by default" YES)

add_subdirectory(Sources)
add_subdirectory(cmake/modules)
4 changes: 4 additions & 0 deletions Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_subdirectory(TSCclibc)
add_subdirectory(TSCLibc)
add_subdirectory(TSCBasic)
add_subdirectory(TSCUtility)
2 changes: 2 additions & 0 deletions Sources/TSCBasic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ target_link_libraries(TSCBasic PUBLIC
set_target_properties(TSCBasic PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

set_property(GLOBAL APPEND PROPERTY TSC_EXPORTS TSCBasic)

install(TARGETS TSCBasic
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
Expand Down
11 changes: 8 additions & 3 deletions Sources/TSCLibc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@

add_library(TSCLibc
libc.swift)
target_compile_options(TSCLibc PRIVATE
-autolink-force-load)

if(CMAKE_SYSTEM_NAME STREQUAL Windows)
target_compile_options(TSCLibc PRIVATE
-autolink-force-load)
endif()
target_link_libraries(TSCLibc PUBLIC
clibc)
TSCclibc)
# NOTE(compnerd) workaround for CMake not setting up include flags yet
set_target_properties(TSCLibc PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

set_property(GLOBAL APPEND PROPERTY TSC_EXPORTS TSCLibc)

install(TARGETS TSCLibc
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
Expand Down
2 changes: 2 additions & 0 deletions Sources/TSCUtility/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ target_link_libraries(TSCUtility PUBLIC
# NOTE(compnerd) workaround for CMake not setting up include flags yet
set_target_properties(TSCUtility PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

set_property(GLOBAL APPEND PROPERTY TSC_EXPORTS TSCUtility)
8 changes: 5 additions & 3 deletions Sources/TSCclibc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
# See http://swift.org/LICENSE.txt for license information
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

add_library(clibc STATIC
add_library(TSCclibc STATIC
libc.c)
target_include_directories(clibc PUBLIC
target_include_directories(TSCclibc PUBLIC
include)

if(NOT BUILD_SHARED_LIBS)
install(TARGETS clibc
install(TARGETS TSCclibc
ARCHIVE DESTINATION lib)
endif()

set_property(GLOBAL APPEND PROPERTY TSC_EXPORTS TSCclibc)
28 changes: 28 additions & 0 deletions Utilities/build-using-cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

set -eu

__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

SRCROOT="`cd "${__dir}/..";pwd`"
echo "SRCROOT is $SRCROOT"

BUILD_DIR=$SRCROOT/build
echo "BUILD_DIR is $BUILD_DIR"

mkdir -p $BUILD_DIR
cd $BUILD_DIR

CMAKE_Swift_FLAGS=""
if (uname | grep -qi darwin); then
CMAKE_Swift_FLAGS="-sdk $(xcrun --sdk macosx --show-sdk-path)"
fi

set -x
cmake \
-G Ninja \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_Swift_FLAGS="$CMAKE_Swift_FLAGS" \
$SRCROOT

ninja
6 changes: 6 additions & 0 deletions cmake/modules/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
set(TSC_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/TSCExports.cmake)
configure_file(TSCConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/TSCConfig.cmake)

get_property(TSC_EXPORTS GLOBAL PROPERTY TSC_EXPORTS)
export(TARGETS ${TSC_EXPORTS} FILE ${TSC_EXPORTS_FILE})
1 change: 1 addition & 0 deletions cmake/modules/TSCConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include(@TSC_EXPORTS_FILE@)

0 comments on commit 54a0b60

Please sign in to comment.