Skip to content

Commit c996fe6

Browse files
Merge pull request swiftlang#76152 from swiftlang/QuietMisdreavus/stdlib-docs
add build-script flags to build/preview stdlib docs with Swift-DocC
2 parents 9c1ec04 + c9cc58c commit c996fe6

File tree

10 files changed

+148
-7
lines changed

10 files changed

+148
-7
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ option(SWIFT_STDLIB_ENABLE_SIB_TARGETS
463463
"Should we generate sib targets for the stdlib or not?"
464464
FALSE)
465465

466+
option(SWIFT_STDLIB_BUILD_SYMBOL_GRAPHS
467+
"Whether to build symbol graphs for the stdlib, for use in documentation."
468+
FALSE)
469+
466470

467471
set(SWIFT_DARWIN_SUPPORTED_ARCHS "" CACHE STRING
468472
"Semicolon-separated list of architectures to configure on Darwin platforms. \

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,13 @@ function(add_swift_target_library_single target name)
937937
set(SWIFTLIB_SINGLE_SOURCES ${SWIFTLIB_SINGLE_SOURCES} ${SWIFTLIB_SINGLE_HEADERS} ${SWIFTLIB_SINGLE_TDS})
938938
endif()
939939

940+
# FIXME: swiftDarwin currently trips an assertion in SymbolGraphGen
941+
if (SWIFTLIB_IS_STDLIB AND SWIFT_STDLIB_BUILD_SYMBOL_GRAPHS AND NOT ${name} STREQUAL "swiftDarwin")
942+
list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS "-Xfrontend;-emit-symbol-graph")
943+
list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS
944+
"-Xfrontend;-emit-symbol-graph-dir;-Xfrontend;${out_lib_dir}/symbol-graph/${VARIANT_NAME}")
945+
endif()
946+
940947
if(MODULE)
941948
set(libkind MODULE)
942949
elseif(SWIFTLIB_SINGLE_OBJECT_LIBRARY)
@@ -2233,7 +2240,6 @@ function(add_swift_target_library name)
22332240
endif()
22342241
endif()
22352242

2236-
22372243
# Collect architecture agnostic SDK linker flags
22382244
set(swiftlib_link_flags_all ${SWIFTLIB_LINK_FLAGS})
22392245
if(sdk STREQUAL "IOS_SIMULATOR" AND name STREQUAL "swiftMediaPlayer")

utils/build_swift/build_swift/driver_arguments.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ def _apply_default_arguments(args):
6060
if args.build_linux_static and args.build_libcxx is None:
6161
args.build_libcxx = True
6262

63+
# Previewing the stdlib docs requires building them.
64+
if args.preview_stdlib_docs:
65+
args.build_stdlib_docs = True
66+
6367
# Set the default CMake generator.
6468
if args.cmake_generator is None:
6569
args.cmake_generator = 'Ninja'
@@ -1160,6 +1164,15 @@ def create_argument_parser():
11601164
help='Include Unicode data in the standard library.'
11611165
'Note: required for full String functionality')
11621166

1167+
option('--build-stdlib-docs', toggle_true,
1168+
default=False,
1169+
help='Build documentation for the standard library.'
1170+
'Note: this builds Swift-DocC to perform the docs build.')
1171+
option('--preview-stdlib-docs', toggle_true,
1172+
default=False,
1173+
help='Build and preview standard library documentation with Swift-DocC.'
1174+
'Note: this builds Swift-DocC to perform the docs build.')
1175+
11631176
option('--build-swift-clang-overlays', toggle_true,
11641177
default=True,
11651178
help='Build Swift overlays for the clang builtin modules')

utils/build_swift/tests/expected_options.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@
274274
'swift_tools_max_parallel_lto_link_jobs':
275275
defaults.SWIFT_MAX_PARALLEL_LTO_LINK_JOBS,
276276
'swift_user_visible_version': defaults.SWIFT_USER_VISIBLE_VERSION,
277+
'build_stdlib_docs': False,
278+
'preview_stdlib_docs': False,
277279
'symbols_package': None,
278280
'clean_libdispatch': True,
279281
'clean_foundation': True,
@@ -578,6 +580,8 @@ class BuildScriptImplOption(_BaseOption):
578580
SetTrueOption('--build-minimal-stdlib', dest='build_minimalstdlib'),
579581
SetTrueOption('--build-wasm-stdlib', dest='build_wasmstdlib'),
580582
SetTrueOption('--wasmkit', dest='build_wasmkit'),
583+
SetTrueOption('--build-stdlib-docs'),
584+
SetTrueOption('--preview-stdlib-docs'),
581585
SetTrueOption('-B', dest='benchmark'),
582586
SetTrueOption('-S', dest='skip_build'),
583587
SetTrueOption('-b', dest='build_llbuild'),

utils/swift_build_support/swift_build_support/build_script_invocation.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,11 +700,19 @@ def compute_product_pipelines(self):
700700
is_enabled=self.args.tsan_libdispatch_test)
701701
builder.add_product(products.SwiftDocC,
702702
is_enabled=self.args.build_swiftdocc)
703-
builder.add_product(products.SwiftDocCRender,
704-
is_enabled=self.args.install_swiftdocc)
705703
builder.add_product(products.MinimalStdlib,
706704
is_enabled=self.args.build_minimalstdlib)
707705

706+
# Swift-DocC-Render should be installed whenever Swift-DocC is installed, which
707+
# can be either given directly or via install-all
708+
install_doccrender = self.args.install_swiftdocc or (
709+
self.args.install_all and self.args.build_swiftdocc
710+
)
711+
builder.add_product(products.SwiftDocCRender,
712+
is_enabled=install_doccrender)
713+
builder.add_product(products.StdlibDocs,
714+
is_enabled=self.args.build_stdlib_docs)
715+
708716
# Keep SwiftDriver at last.
709717
# swift-driver's integration with the build scripts is not fully
710718
# supported. Using swift-driver to build these products may hit

utils/swift_build_support/swift_build_support/products/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from .skstresstester import SKStressTester
3030
from .sourcekitlsp import SourceKitLSP
3131
from .staticswiftlinux import StaticSwiftLinuxConfig
32+
from .stdlib_docs import StdlibDocs
3233
from .swift import Swift
3334
from .swift_testing import SwiftTesting
3435
from .swift_testing_macros import SwiftTestingMacros
@@ -65,6 +66,7 @@
6566
'Ninja',
6667
'PlaygroundSupport',
6768
'StaticSwiftLinuxConfig',
69+
'StdlibDocs',
6870
'Swift',
6971
'SwiftFormat',
7072
'SwiftInspect',
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# swift_build_support/products/stdlib_docs.py -------------------*- python -*-
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See https://swift.org/LICENSE.txt for license information
9+
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
#
11+
# ----------------------------------------------------------------------------
12+
13+
import os
14+
15+
from . import product
16+
from . import swiftdocc
17+
from . import swiftdoccrender
18+
from .. import shell
19+
20+
21+
class StdlibDocs(product.Product):
22+
@classmethod
23+
def is_build_script_impl_product(cls):
24+
return False
25+
26+
@classmethod
27+
def is_before_build_script_impl_product(cls):
28+
return False
29+
30+
@classmethod
31+
def is_swiftpm_unified_build_product(cls):
32+
return False
33+
34+
def should_build(self, host_target):
35+
return self.args.build_stdlib_docs
36+
37+
def build(self, host_target):
38+
toolchain_path = self.install_toolchain_path(host_target)
39+
docc_path = os.path.join(toolchain_path, "bin", "docc")
40+
41+
swift_build_dir = os.path.join(
42+
os.path.dirname(self.build_dir),
43+
f'swift-{host_target}'
44+
)
45+
symbol_graph_dir = os.path.join(swift_build_dir, "lib", "symbol-graph")
46+
output_path = os.path.join(swift_build_dir, "Swift.doccarchive")
47+
48+
docc_action = 'preview' if self.args.preview_stdlib_docs else 'convert'
49+
50+
docc_cmd = [
51+
docc_path,
52+
docc_action,
53+
"--additional-symbol-graph-dir",
54+
symbol_graph_dir,
55+
"--output-path",
56+
output_path,
57+
"--default-code-listing-language",
58+
"swift",
59+
"--fallback-display-name",
60+
"Swift",
61+
"--fallback-bundle-identifier",
62+
"org.swift.swift",
63+
]
64+
65+
shell.call(docc_cmd)
66+
67+
def should_test(self, host_target):
68+
return False
69+
70+
def should_install(self, host_target):
71+
return False
72+
73+
@classmethod
74+
def get_dependencies(cls):
75+
"""Return a list of products that this product depends upon"""
76+
return [
77+
swiftdocc.SwiftDocC,
78+
swiftdoccrender.SwiftDocCRender
79+
]

utils/swift_build_support/swift_build_support/products/swift.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ def __init__(self, args, toolchain, source_dir, build_dir):
8989

9090
self.cmake_options.extend(self._enable_embedded_stdlib_cross_compiling)
9191

92+
self.cmake_options.extend(self._enable_stdlib_symbol_graphs)
93+
9294
self.cmake_options.extend(
9395
self._swift_tools_ld64_lto_codegen_only_for_supporting_targets)
9496

@@ -271,6 +273,11 @@ def _enable_embedded_stdlib_cross_compiling(self):
271273
return [('SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB_CROSS_COMPILING',
272274
self.args.build_embedded_stdlib_cross_compiling)]
273275

276+
@property
277+
def _enable_stdlib_symbol_graphs(self):
278+
return [('SWIFT_STDLIB_BUILD_SYMBOL_GRAPHS:BOOL',
279+
self.args.build_stdlib_docs)]
280+
274281
def _handle_swift_debuginfo_non_lto_args(self):
275282
if ('swift_debuginfo_non_lto_args' not in self.args
276283
or self.args.swift_debuginfo_non_lto_args is None):

utils/swift_build_support/swift_build_support/products/swiftdoccrender.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ def should_test(self, host_target):
4848

4949
def should_install(self, host_target):
5050
# Swift-DocC-Render should always be installed if Swift-DocC is being installed
51-
return self.args.install_swiftdocc
51+
return self.args.install_swiftdocc or (
52+
self.args.install_all and self.args.build_swiftdocc
53+
)
5254

5355
def install(self, host_target):
5456
# Swift-DocC-Render is installed at '/usr/share/docc/render' in the built

utils/swift_build_support/tests/products/test_swift.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ def setUp(self):
7171
build_embedded_stdlib_cross_compiling=False,
7272
swift_freestanding_is_darwin=False,
7373
build_swift_private_stdlib=True,
74-
swift_tools_ld64_lto_codegen_only_for_supporting_targets=False)
74+
swift_tools_ld64_lto_codegen_only_for_supporting_targets=False,
75+
build_stdlib_docs=False)
7576

7677
# Setup shell
7778
shell.dry_run = True
@@ -120,7 +121,8 @@ def test_by_default_no_cmake_options(self):
120121
'-DSWIFT_SHOULD_BUILD_EMBEDDED_STDLIB=TRUE',
121122
'-DSWIFT_SHOULD_BUILD_EMBEDDED_STDLIB_CROSS_COMPILING=FALSE',
122123
'-DSWIFT_TOOLS_LD64_LTO_CODEGEN_ONLY_FOR_SUPPORTING_TARGETS:BOOL=FALSE',
123-
'-USWIFT_DEBUGINFO_NON_LTO_ARGS'
124+
'-USWIFT_DEBUGINFO_NON_LTO_ARGS',
125+
'-DSWIFT_STDLIB_BUILD_SYMBOL_GRAPHS:BOOL=FALSE'
124126
]
125127
self.assertEqual(set(swift.cmake_options), set(expected))
126128

@@ -154,7 +156,8 @@ def test_swift_runtime_tsan(self):
154156
'-DSWIFT_SHOULD_BUILD_EMBEDDED_STDLIB=TRUE',
155157
'-DSWIFT_SHOULD_BUILD_EMBEDDED_STDLIB_CROSS_COMPILING=FALSE',
156158
'-DSWIFT_TOOLS_LD64_LTO_CODEGEN_ONLY_FOR_SUPPORTING_TARGETS:BOOL=FALSE',
157-
'-USWIFT_DEBUGINFO_NON_LTO_ARGS'
159+
'-USWIFT_DEBUGINFO_NON_LTO_ARGS',
160+
'-DSWIFT_STDLIB_BUILD_SYMBOL_GRAPHS:BOOL=FALSE'
158161
]
159162
self.assertEqual(set(swift.cmake_options), set(flags_set))
160163

@@ -567,3 +570,16 @@ def test_swift_debuginfo_non_lto_args(self):
567570
'-gline-tables-only;-v'],
568571
[x for x in swift.cmake_options
569572
if 'SWIFT_DEBUGINFO_NON_LTO_ARGS' in x])
573+
574+
def test_stdlib_docs_flags(self):
575+
self.args.build_stdlib_docs = True
576+
swift = Swift(
577+
args=self.args,
578+
toolchain=self.toolchain,
579+
source_dir='/path/to/src',
580+
build_dir='/path/to/build')
581+
self.assertEqual(
582+
['-DSWIFT_STDLIB_BUILD_SYMBOL_GRAPHS:BOOL='
583+
'TRUE'],
584+
[x for x in swift.cmake_options
585+
if 'DSWIFT_STDLIB_BUILD_SYMBOL_GRAPHS' in x])

0 commit comments

Comments
 (0)