Skip to content

Commit

Permalink
Introduce a CMake build system for Swift.
Browse files Browse the repository at this point in the history
This CMake-based build system is based on the one in Clang, simplified
and tweaked slightly to better support building a smaller Xcode
project that links against an existing LLVM (rather than importing all
of LLVM into this project).



Swift SVN r403
  • Loading branch information
DougGregor committed Jul 14, 2011
1 parent 023c9cc commit 86ab76d
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 0 deletions.
167 changes: 167 additions & 0 deletions CMakeLists.txt
@@ -0,0 +1,167 @@
# Support building Swift as a standalone project, using LLVM as an
# external library.
if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
project(Swift)
cmake_minimum_required(VERSION 2.8)

set(SWIFT_PATH_TO_LLVM_SOURCE "" CACHE PATH
"Path to LLVM source code. Not necessary if using an installed LLVM.")
set(SWIFT_PATH_TO_LLVM_BUILD "" CACHE PATH
"Path to the directory where LLVM was built or installed.")

if( SWIFT_PATH_TO_LLVM_SOURCE )
if( NOT EXISTS "${SWIFT_PATH_TO_LLVM_SOURCE}/cmake/config-ix.cmake" )
message(FATAL_ERROR "Please set SWIFT_PATH_TO_LLVM_SOURCE to the root directory of LLVM source code.")
else()
get_filename_component(LLVM_MAIN_SRC_DIR ${SWIFT_PATH_TO_LLVM_SOURCE}
ABSOLUTE)
list(APPEND CMAKE_MODULE_PATH "${LLVM_MAIN_SRC_DIR}/cmake/modules")
endif()
endif()

# FIXME: The check for a 'Debug' tblgen is madness
if( NOT EXISTS "${SWIFT_PATH_TO_LLVM_BUILD}/bin/tblgen${CMAKE_EXECUTABLE_SUFFIX}" )
if ( NOT EXISTS "${SWIFT_PATH_TO_LLVM_BUILD}/bin/Debug/tblgen${CMAKE_EXECUTABLE_SUFFIX}" )
message(FATAL_ERROR "Please set SWIFT_PATH_TO_LLVM_BUILD to a directory containing an LLVM build.")
endif()
endif()

list(APPEND CMAKE_MODULE_PATH "${SWIFT_PATH_TO_LLVM_BUILD}/share/llvm/cmake")

get_filename_component(PATH_TO_LLVM_BUILD ${SWIFT_PATH_TO_LLVM_BUILD}
ABSOLUTE)

include(AddLLVM)
include(LLVMParseArguments)
include(TableGen)
include("${SWIFT_PATH_TO_LLVM_BUILD}/share/llvm/cmake/LLVMConfig.cmake")
include(HandleLLVMOptions)

set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")

set(LLVM_MAIN_INCLUDE_DIR "${LLVM_MAIN_SRC_DIR}/include")
set(LLVM_BINARY_DIR ${CMAKE_BINARY_DIR})
set(LLVM_TOOLS_BINARY_DIR ${CMAKE_BINARY_DIR})

set(CMAKE_INCLUDE_CURRENT_DIR ON)
include_directories("${PATH_TO_LLVM_BUILD}/include" "${LLVM_MAIN_INCLUDE_DIR}")
link_directories("${PATH_TO_LLVM_BUILD}/lib")

if( EXISTS "${SWIFT_PATH_TO_LLVM_BUILD}/bin/tblgen${CMAKE_EXECUTABLE_SUFFIX}" )
set(LLVM_TABLEGEN_EXE "${PATH_TO_LLVM_BUILD}/bin/tblgen")
else()
# FIXME: The fallback to a 'Debug' tblgen is madness
set(LLVM_TABLEGEN_EXE "${PATH_TO_LLVM_BUILD}/bin/Debug/tblgen")
endif()

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

set( SWIFT_BUILT_STANDALONE 1 )
endif()

set(SWIFT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(SWIFT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})

# Add a new Swift library.
#
# Usage:
# add_swift_library(name # Name of the library (e.g., swiftParse)
# [DEPENDS dep1 ...] # Libraries this library depends on
# [COMPONENT_DEPENDS comp1 ...] # LLVM components this library depends on
# source1 [source2 source3 ...]) # Sources to add into this library
macro(add_swift_library name)
parse_arguments(SWIFTLIB "DEPENDS;COMPONENT_DEPENDS" "" ${ARGN})
set(SWIFTLIB_SOURCES ${SWIFTLIB_DEFAULT_ARGS})

if(MSVC_IDE OR XCODE)
string( REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
list( GET split_path -1 dir)
file( GLOB_RECURSE SWIFTLIB_HEADERS
${SWIFT_SOURCE_DIR}/include/swift${dir}/*.h
${SWIFT_SOURCE_DIR}/include/swift${dir}/*.td
${SWIFT_SOURCE_DIR}/include/swift${dir}/*.def)
set(SWIFTLIB_SOURCES ${SWIFTLIB_SOURCES} ${SWIFTLIB_HEADERS})
endif(MSVC_IDE OR XCODE)

if (MODULE)
set(libkind MODULE)
elseif (SHARED_LIBRARY)
set(libkind SHARED)
else()
set(libkind)
endif()

add_library( ${name} ${libkind} ${SWIFTLIB_SOURCES} )

if( LLVM_COMMON_DEPENDS )
add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
endif( LLVM_COMMON_DEPENDS )

target_link_libraries( ${name} ${SWIFTLIB_DEPENDS} )
llvm_config( ${name} ${SWIFTLIB_COMPONENT_DEPENDS} )
target_link_libraries( ${name} ${LLVM_COMMON_LIBS} )
link_system_libs( ${name} )

if(MSVC)
get_target_property(cflag ${name} COMPILE_FLAGS)
if(NOT cflag)
set(cflag "")
endif(NOT cflag)
set(cflag "${cflag} /Za")
set_target_properties(${name} PROPERTIES COMPILE_FLAGS ${cflag})
endif(MSVC)

install(TARGETS ${name}
LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})

set_target_properties(${name} PROPERTIES FOLDER "Swift libraries")
endmacro(add_swift_library)

# Add a new Swift executable.
#
# Usage:
# add_swift_executable(name # Name of the executable (e.g., swift)
# [DEPENDS dep1 ...] # Libraries this executable depends on
# [COMPONENT_DEPENDS comp1 ...] # LLVM components this executable
# # depends on
# [EXCLUDE_FROM_ALL] # Whether to exclude this executable from
# # the ALL_BUILD target
# source1 [source2 source3 ...]) # Sources to add into this library
macro(add_swift_executable name)
# Parse the arguments we were given.
parse_arguments(SWIFTEXE "DEPENDS;COMPONENT_DEPENDS" "EXCLUDE_FROM_ALL"
${ARGN})

# Add the executable
if (${SWIFTEXE_EXCLUDE_FROM_ALL})
add_executable(${name} EXCLUDE_FROM_ALL ${SWIFTEXE_DEFAULT_ARGS})
else()
add_executable(${name} ${SWIFTEXE_DEFAULT_ARGS})
endif()

# Add appropriate dependencies
if( LLVM_COMMON_DEPENDS )
add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
endif( LLVM_COMMON_DEPENDS )

target_link_libraries( ${name} ${SWIFTEXE_DEPENDS} )
llvm_config( ${name} ${SWIFTEXE_COMPONENT_DEPENDS} )
target_link_libraries( ${name} ${LLVM_COMMON_LIBS} )
link_system_libs( ${name} )

set_target_properties(${name} PROPERTIES FOLDER "Swift executables")
endmacro(add_swift_executable)

# We'll need this once we have generated headers
include_directories(BEFORE
${CMAKE_CURRENT_BINARY_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/include
)

# Add all of the subdirectories, where we actually do work.
add_subdirectory(lib)
add_subdirectory(tools)
add_subdirectory(test)
7 changes: 7 additions & 0 deletions lib/AST/CMakeLists.txt
@@ -0,0 +1,7 @@
add_swift_library(swiftAST
ASTContext.cpp
Decl.cpp
Expr.cpp
Identifier.cpp
Type.cpp)

3 changes: 3 additions & 0 deletions lib/CMakeLists.txt
@@ -0,0 +1,3 @@
add_subdirectory(AST)
add_subdirectory(Sema)
add_subdirectory(Parse)
4 changes: 4 additions & 0 deletions lib/Parse/CMakeLists.txt
@@ -0,0 +1,4 @@
add_swift_library(swiftParse
Lexer.cpp
Parser.cpp
DEPENDS swiftSema swiftAST)
10 changes: 10 additions & 0 deletions lib/Sema/CMakeLists.txt
@@ -0,0 +1,10 @@
add_swift_library(swiftSema
NameBinding.cpp
Scope.cpp
Sema.cpp
SemaBase.cpp
SemaDecl.cpp
SemaExpr.cpp
SemaType.cpp
TypeChecking.cpp
DEPENDS swiftAST)
21 changes: 21 additions & 0 deletions test/lit.site.cfg.in
@@ -0,0 +1,21 @@
## Autogenerated by Swift configuration.
# Do not edit!
config.llvm_src_root = "@LLVM_MAIN_SRC_DIR@"
config.llvm_obj_root = "@LLVM_BINARY_DIR@"
config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
config.llvm_libs_dir = "@LLVM_LIBS_DIR@"
config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@"
config.swift_obj_root = "@SWIFT_BINARY_DIR@"
config.target_triple = "@TARGET_TRIPLE@"

# Support substitution of the tools and libs dirs with user parameters. This is
# used when we can't determine the tool dir at configuration time.
try:
config.llvm_tools_dir = config.llvm_tools_dir % lit.params
config.llvm_libs_dir = config.llvm_libs_dir % lit.params
except KeyError,e:
key, = e.args
lit.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key,key))

# Let the main config do the real work.
lit.load_config(config, "@SWIFT_SOURCE_DIR@/test/lit.cfg")
7 changes: 7 additions & 0 deletions tools/swift/CMakeLists.txt
@@ -0,0 +1,7 @@
add_swift_executable(swift
swift.cpp
DEPENDS swiftParse swiftSema swiftAST
COMPONENT_DEPENDS bitwriter codegen ipo selectiondag ${LLVM_TARGETS_TO_BUILD})

install(TARGETS swift
RUNTIME DESTINATION bin)

0 comments on commit 86ab76d

Please sign in to comment.