Skip to content

Commit a680aed

Browse files
committed
build: add a CMake based build
This is meant to support building swift-markdown for re-use in the toolchain build. It does not follow best practices (opting for a single file approach rather than a CMakeLists.txt per directory) and currently needs some refinement for integrating an existing CMark build, but provides the skeleton for further experimentation.
1 parent c211079 commit a680aed

8 files changed

+315
-0
lines changed

CMakeLists.txt

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2024 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
cmake_minimum_required(VERSION 3.19.0)
11+
12+
if(POLICY CMP0077)
13+
cmake_policy(SET CMP0077 NEW)
14+
endif()
15+
16+
if(POLICY CMP0091)
17+
cmake_policy(SET CMP0091 NEW)
18+
endif()
19+
20+
project(SwiftMarkdown
21+
LANGUAGES C Swift)
22+
23+
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)
24+
25+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
26+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
27+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
28+
29+
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
30+
set(CMAKE_Swift_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
31+
32+
include(FetchContent)
33+
include(SwiftSupport)
34+
35+
set(_SM_VENDOR_DEPENDENCIES)
36+
37+
set(BUILD_EXAMPLES NO)
38+
set(BUILD_TESTING NO)
39+
40+
find_package(ArgumentParser CONFIG)
41+
if(NOT ArgumentParser_FOUND)
42+
FetchContent_Declare(ArgumentParser
43+
GIT_REPOSITORY https://github.com/apple/swift-argument-parser
44+
GIT_TAG 1.2.3)
45+
list(APPEND _SM_VENDOR_DEPENDENCIES ArgumentParser)
46+
endif()
47+
48+
find_package(cmark-gfm CONFIG)
49+
if(NOT cmark-gfm_FOUND)
50+
FetchContent_Declare(cmark-gfm
51+
GIT_REPOSITORY https://github.com/apple/swift-cmark
52+
GIT_TAG gfm)
53+
list(APPEND _SM_VENDOR_DEPENDENCIES cmark-gfm)
54+
endif()
55+
56+
if(_SM_VENDOR_DEPENDENCIES)
57+
FetchContent_MakeAvailable(${_SM_VENDOR_DEPENDENCIES})
58+
endif()
59+
60+
add_subdirectory(Sources)
61+
add_subdirectory(cmake/modules)

Sources/CAtomic/CMakeLists.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2024 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
add_library(CAtomic STATIC
11+
CAtomic.c)
12+
target_include_directories(CAtomic PUBLIC
13+
include)
14+
15+
set_property(GLOBAL APPEND PROPERTY SWIFT_MARKDOWN_EXPORTS CAtomic)
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module CAtomic {
2+
header "CAtomic.h"
3+
export *
4+
}

Sources/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2024 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
add_subdirectory(CAtomic)
11+
add_subdirectory(Markdown)

Sources/Markdown/CMakeLists.txt

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2024 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
add_library(Markdown
11+
Base/ChildIndexPath.swift
12+
Base/DirectiveArgument.swift
13+
Base/Document.swift
14+
Base/LiteralMarkup.swift
15+
Base/Markup.swift
16+
Base/MarkupChildren.swift
17+
Base/MarkupData.swift
18+
Base/PlainTextConvertibleMarkup.swift
19+
Base/RawMarkup.swift
20+
"Block Nodes/Block Container Blocks/Doxygen Commands/DoxygenParameter.swift"
21+
"Block Nodes/Block Container Blocks/Doxygen Commands/DoxygenReturns.swift"
22+
"Block Nodes/Block Container Blocks/BlockDirective.swift"
23+
"Block Nodes/Block Container Blocks/BlockQuote.swift"
24+
"Block Nodes/Block Container Blocks/CustomBlock.swift"
25+
"Block Nodes/Block Container Blocks/ListItem.swift"
26+
"Block Nodes/Block Container Blocks/OrderedList.swift"
27+
"Block Nodes/Block Container Blocks/UnorderedList.swift"
28+
"Block Nodes/Inline Container Blocks/Paragraph.swift"
29+
"Block Nodes/Leaf Blocks/CodeBlock.swift"
30+
"Block Nodes/Leaf Blocks/Heading.swift"
31+
"Block Nodes/Leaf Blocks/HTMLBlock.swift"
32+
"Block Nodes/Leaf Blocks/ThematicBreak.swift"
33+
"Block Nodes/Tables/Table.swift"
34+
"Block Nodes/Tables/TableBody.swift"
35+
"Block Nodes/Tables/TableCell.swift"
36+
"Block Nodes/Tables/TableCellContainer.swift"
37+
"Block Nodes/Tables/TableHead.swift"
38+
"Block Nodes/Tables/TableRow.swift"
39+
Infrastructure/Replacement.swift
40+
Infrastructure/SourceLocation.swift
41+
"Inline Nodes/Inline Containers/Emphasis.swift"
42+
"Inline Nodes/Inline Containers/Image.swift"
43+
"Inline Nodes/Inline Containers/InlineAttributes.swift"
44+
"Inline Nodes/Inline Containers/Link.swift"
45+
"Inline Nodes/Inline Containers/Strikethrough.swift"
46+
"Inline Nodes/Inline Containers/Strong.swift"
47+
"Inline Nodes/Inline Leaves/CustomInline.swift"
48+
"Inline Nodes/Inline Leaves/InlineCode.swift"
49+
"Inline Nodes/Inline Leaves/InlineHTML.swift"
50+
"Inline Nodes/Inline Leaves/LineBreak.swift"
51+
"Inline Nodes/Inline Leaves/SoftBreak.swift"
52+
"Inline Nodes/Inline Leaves/SymbolLink.swift"
53+
"Inline Nodes/Inline Leaves/Text.swift"
54+
"Interpretive Nodes/Aside.swift"
55+
Parser/BlockDirectiveParser.swift
56+
Parser/CommonMarkConverter.swift
57+
Parser/LazySplitLines.swift
58+
Parser/ParseOptions.swift
59+
Parser/RangeAdjuster.swift
60+
Parser/RangerTracker.swift
61+
Rewriter/MarkupRewriter.swift
62+
"Structural Restrictions/BasicBlockContainer.swift"
63+
"Structural Restrictions/BasicInlineContainer.swift"
64+
"Structural Restrictions/BlockContainer.swift"
65+
"Structural Restrictions/BlockMarkup.swift"
66+
"Structural Restrictions/InlineContainer.swift"
67+
"Structural Restrictions/InlineMarkup.swift"
68+
"Structural Restrictions/ListItemContainer.swift"
69+
Utility/AtomicCounter.swift
70+
Utility/CharacterExtensions.swift
71+
Utility/CollectionExtensions.swift
72+
Utility/StringExtensions.swift
73+
Visitor/MarkupVisitor.swift
74+
Walker/MarkupWalker.swift
75+
Walker/Walkers/MarkupFormatter.swift
76+
Walker/Walkers/MarkupTreeDumper.swift)
77+
target_link_libraries(Markdown PRIVATE
78+
ArgumentParser
79+
libcmark-gfm
80+
libcmark-gfm-extensions)
81+
target_link_libraries(Markdown PUBLIC
82+
CAtomic)
83+
set_target_properties(Markdown PROPERTIES
84+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
85+
86+
_install_target(Markdown)
87+
set_property(GLOBAL APPEND PROPERTY SWIFT_MARKDOWN_EXPORTS Markdown)

cmake/modules/CMakeLists.txt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2024 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
set(SWIFT_MARKDOWN_EXPORTS_FILE
11+
${CMAKE_CURRENT_BINARY_DIR}/SwiftMarkdownExports.cmake)
12+
13+
configure_file(SwiftMarkdownConfig.cmake.in
14+
${CMAKE_CURRENT_BINARY_DIR}/SwiftMarkdownConfig.cmake)
15+
16+
get_property(SWIFT_MARKDOWN_EXPORTS GLOBAL PROPERTY SWIFT_MARKDOWN_EXPORTS)
17+
export(TARGETS ${SWIFT_MARKDOWN_EXPORTS}
18+
NAMESPACE SwiftMarkdown::
19+
FILE ${SWIFT_MARKDOWN_EXPORTS_FILE}
20+
EXPORT_LINK_INTERFACE_LIBRARIES)
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2024 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
if(NOT TARGET SwiftMarkdown)
11+
include("@SWIFT_MARKDOWN_EXPORTS_FILE@")
12+
endif()

cmake/modules/SwiftSupport.cmake

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2024 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
# Returns the current architecture name in a variable
11+
#
12+
# Usage:
13+
# get_swift_host_arch(result_var_name)
14+
#
15+
# If the current architecture is supported by Swift, sets ${result_var_name}
16+
# with the sanitized host architecture name derived from CMAKE_SYSTEM_PROCESSOR.
17+
function(get_swift_host_arch result_var_name)
18+
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
19+
set("${result_var_name}" "x86_64" PARENT_SCOPE)
20+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
21+
set("${result_var_name}" "aarch64" PARENT_SCOPE)
22+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64")
23+
set("${result_var_name}" "powerpc64" PARENT_SCOPE)
24+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le")
25+
set("${result_var_name}" "powerpc64le" PARENT_SCOPE)
26+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x")
27+
set("${result_var_name}" "s390x" PARENT_SCOPE)
28+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l")
29+
set("${result_var_name}" "armv6" PARENT_SCOPE)
30+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7l")
31+
set("${result_var_name}" "armv7" PARENT_SCOPE)
32+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7-a")
33+
set("${result_var_name}" "armv7" PARENT_SCOPE)
34+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "amd64")
35+
set("${result_var_name}" "amd64" PARENT_SCOPE)
36+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64")
37+
set("${result_var_name}" "x86_64" PARENT_SCOPE)
38+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "IA64")
39+
set("${result_var_name}" "itanium" PARENT_SCOPE)
40+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86")
41+
set("${result_var_name}" "i686" PARENT_SCOPE)
42+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686")
43+
set("${result_var_name}" "i686" PARENT_SCOPE)
44+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "wasm32")
45+
set("${result_var_name}" "wasm32" PARENT_SCOPE)
46+
else()
47+
message(FATAL_ERROR "Unrecognized architecture on host system: ${CMAKE_SYSTEM_PROCESSOR}")
48+
endif()
49+
endfunction()
50+
51+
# Returns the os name in a variable
52+
#
53+
# Usage:
54+
# get_swift_host_os(result_var_name)
55+
#
56+
#
57+
# Sets ${result_var_name} with the converted OS name derived from
58+
# CMAKE_SYSTEM_NAME.
59+
function(get_swift_host_os result_var_name)
60+
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
61+
set(${result_var_name} macosx PARENT_SCOPE)
62+
else()
63+
string(TOLOWER ${CMAKE_SYSTEM_NAME} cmake_system_name_lc)
64+
set(${result_var_name} ${cmake_system_name_lc} PARENT_SCOPE)
65+
endif()
66+
endfunction()
67+
68+
function(_install_target module)
69+
get_swift_host_os(swift_os)
70+
get_target_property(type ${module} TYPE)
71+
72+
if(type STREQUAL STATIC_LIBRARY)
73+
set(swift swift_static)
74+
else()
75+
set(swift swift)
76+
endif()
77+
78+
install(TARGETS ${module}
79+
ARCHIVE DESTINATION lib/${swift}/${swift_os}
80+
LIBRARY DESTINATION lib/${swift}/${swift_os}
81+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
82+
if(type STREQUAL EXECUTABLE)
83+
return()
84+
endif()
85+
86+
get_swift_host_arch(swift_arch)
87+
get_target_property(module_name ${module} Swift_MODULE_NAME)
88+
if(NOT module_name)
89+
set(module_name ${module})
90+
endif()
91+
92+
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
93+
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
94+
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
95+
RENAME ${swift_arch}.swiftdoc)
96+
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
97+
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
98+
RENAME ${swift_arch}.swiftmodule)
99+
else()
100+
install(FILES
101+
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
102+
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
103+
DESTINATION lib/${swift}/${swift_os}/${swift_arch})
104+
endif()
105+
endfunction()

0 commit comments

Comments
 (0)