├── .bazelversion ├── .github └── workflows │ └── pr-build-test.yaml ├── .gitignore ├── LICENSE ├── MODULE.bazel ├── README.md ├── WORKSPACE ├── cc_module ├── BUILD ├── defs.bzl └── private │ ├── BUILD │ ├── cc_module.bzl │ ├── cc_module_archive.bzl │ ├── cc_module_compile.bzl │ ├── cc_module_link.bzl │ ├── provider.bzl │ └── utility.bzl ├── ci ├── Dockerfile ├── install_bazel.sh ├── install_clang.sh ├── run_docker.sh └── setup_build_environment.sh ├── example ├── hello-world │ ├── BUILD │ ├── hello.ixx │ └── main.cc ├── module-library │ ├── BUILD │ ├── a.cc │ ├── b.cc │ ├── b.h │ └── main.cc ├── multi_src_module │ ├── BUILD │ ├── main.cc │ ├── spanish_english_dictionary.cc │ ├── spanish_english_dictionary_impl.cc │ ├── speech.cc │ └── speech_impl.cc ├── template-module │ ├── BUILD │ ├── algorithm.cc │ └── main.cc └── transitive │ ├── BUILD │ ├── a.cc │ ├── b.cc │ └── main.cc └── util └── driver ├── BUILD ├── __pycache__ ├── clang.cpython-36.pyc └── gcc.cpython-36.pyc ├── clang.py ├── driver.py └── gcc.py /.bazelversion: -------------------------------------------------------------------------------- 1 | 5.0.0 2 | -------------------------------------------------------------------------------- /.github/workflows/pr-build-test.yaml: -------------------------------------------------------------------------------- 1 | name: pr-build-test 2 | on: [push] 3 | 4 | jobs: 5 | build-and-test: 6 | runs-on: ubuntu-21.04 7 | steps: 8 | - name: Checkout code 9 | uses: actions/checkout@v2 10 | - name: setup build environment 11 | run: | 12 | ./ci/setup_build_environment.sh 13 | ./ci/install_bazel.sh 14 | ./ci/install_clang.sh 15 | - name: Build 16 | run: bazel build //... 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | bazel-* 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /MODULE.bazel: -------------------------------------------------------------------------------- 1 | bazel_dep(name = "rules_cc", version = "0.1.1") 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rules_cc_module 2 | 3 | Rules to use C++20 modules with Bazel. 4 | 5 | ## Getting started 6 | 7 | Note: Currently only works with a recent version of clang. 8 | 9 | Build a simple module: 10 | ```bazel 11 | cc_module( 12 | name = "Hello", 13 | src = "say_hello.cc", # say_hello exports the module Hello 14 | ) 15 | 16 | # Build a binary with the module 17 | cc_module_binary( 18 | name = "a.out", 19 | srcs = [ 20 | "main.cc", # We can import Hello in main.cc 21 | ], 22 | deps = [ 23 | ":Hello", 24 | ], 25 | ) 26 | ``` 27 | 28 | Build a module with implementation units: 29 | ```bazel 30 | cc_module_binary( 31 | name = "speech", 32 | src = "speech.cc", # speech.cc exports the module speech 33 | impl_srcs = [ 34 | "speech_impl.cc", # speech_impl.cc provides implements (but doesn't export) speech 35 | ], 36 | ) 37 | ``` 38 | 39 | Interoperate with regular cc libraries 40 | ```bazel 41 | cc_module( 42 | name = "a", 43 | src = "a.cc", 44 | ) 45 | 46 | cc_module_library( 47 | name = "b", 48 | hdrs = [ 49 | "b.h", 50 | ], 51 | srcs = [ 52 | "b.cc", # b can import module a, but shouldn't export a module 53 | ], 54 | deps = [ 55 | ":a", 56 | ], 57 | ) 58 | 59 | # We can use b with regular cc rules 60 | cc_binary( 61 | name = "a.out", 62 | srcs = [ 63 | "main.cc", 64 | ], 65 | deps = [ 66 | ":b", 67 | ], 68 | ) 69 | ``` 70 | 71 | ## Documentation 72 | [How to Use C++20 Modules with Bazel](https://buildingblock.ai/cpp20-modules-bazel) 73 | 74 | ## Examples 75 | The directory [example](https://github.com/rnburn/bazel-cpp20-modules/tree/main/example) demonstrates 76 | usage and there is a docker image that provides a build environment. To build the examples, 77 | run 78 | ``` 79 | ./ci/run_docker.sh # spins up a build environment 80 | bazel build //example/... # build the examples 81 | ``` 82 | -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- 1 | workspace(name = "com_github_rnburn_bazel_cpp20_modules") 2 | 3 | load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") 4 | 5 | git_repository( 6 | name = "bazel_skylib", 7 | commit = "f80bc733d4b9f83d427ce3442be2e07427b2cc8d", 8 | remote = "http://github.com/bazelbuild/bazel-skylib" 9 | ) 10 | 11 | load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") 12 | 13 | bazel_skylib_workspace() 14 | -------------------------------------------------------------------------------- /cc_module/BUILD: -------------------------------------------------------------------------------- 1 | load("@bazel_skylib//:bzl_library.bzl", "bzl_library") 2 | 3 | package(default_visibility = ["//visibility:public"]) 4 | 5 | exports_files([ 6 | "defs.bzl", 7 | ]) 8 | 9 | bzl_library( 10 | name = "rules", 11 | srcs = glob(["**/*.bzl"]), 12 | deps = [ 13 | # "//cc_module/platform:rules", 14 | # "//cc_module/private:rules", 15 | ], 16 | ) 17 | -------------------------------------------------------------------------------- /cc_module/defs.bzl: -------------------------------------------------------------------------------- 1 | load("//cc_module/private:cc_module.bzl", 2 | _cc_module = "cc_module", 3 | _cc_header_module = "cc_header_module", 4 | _cc_module_binary = "cc_module_binary", 5 | _cc_module_library = "cc_module_library", 6 | ) 7 | 8 | cc_module = _cc_module 9 | cc_header_module = _cc_header_module 10 | cc_module_binary = _cc_module_binary 11 | cc_module_library = _cc_module_library 12 | -------------------------------------------------------------------------------- /cc_module/private/BUILD: -------------------------------------------------------------------------------- 1 | load("@bazel_skylib//:bzl_library.bzl", "bzl_library") 2 | 3 | bzl_library( 4 | name = "rules", 5 | srcs = glob( 6 | ["**/*.bzl"], 7 | ), 8 | visibility = ["//cc_module:__subpackages__"], 9 | ) 10 | -------------------------------------------------------------------------------- /cc_module/private/cc_module.bzl: -------------------------------------------------------------------------------- 1 | # Copyright 2019 The Bazel Authors. All rights reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | load("//cc_module/private:cc_module_archive.bzl", "cc_module_archive_action") 16 | load("//cc_module/private:cc_module_compile.bzl", 17 | "cc_module_compile_action", 18 | "cc_header_module_compile_action", 19 | ) 20 | load("//cc_module/private:cc_module_link.bzl", "cc_module_link_action") 21 | load("//cc_module/private:utility.bzl", 22 | "get_cc_info_deps", 23 | "get_module_deps", 24 | "get_header_module_name", 25 | "make_module_mapper", 26 | "make_module_compilation_context", 27 | ) 28 | load("//cc_module/private:provider.bzl", "ModuleCompileInfo", "ModuleCompilationContext") 29 | 30 | _common_attrs = { 31 | "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), 32 | "_driver": attr.label( 33 | default = Label("//util/driver"), 34 | executable = True, 35 | cfg = "exec", 36 | ), 37 | "deps": attr.label_list(), 38 | "copts": attr.string_list(), 39 | "linkopts": attr.string_list(), 40 | } 41 | 42 | ########################################################################################### 43 | # cc_module 44 | ########################################################################################### 45 | def compile_module_impl_srcs(ctx, module_name, module_out_file): 46 | impl_deps = ctx.attr.deps + ctx.attr.impl_deps 47 | cc_info_impl_deps = get_cc_info_deps(impl_deps) 48 | module_impl_deps = get_module_deps(impl_deps) 49 | module_info = ModuleCompileInfo( 50 | module_name = module_name, 51 | module_file = module_out_file, 52 | module_dependencies = module_impl_deps, 53 | ) 54 | module_map = make_module_mapper( 55 | ctx.label.name + "-impl_srcs", 56 | ctx.actions, 57 | module_impl_deps) 58 | compilation_context = make_module_compilation_context( 59 | cc_info_impl_deps, module_map, module_impl_deps) 60 | compilation_context = ModuleCompilationContext( 61 | compilation_context = compilation_context.compilation_context, 62 | module_mapper = compilation_context.module_mapper, 63 | module_inputs = depset( 64 | direct = [module_out_file], 65 | transitive = [compilation_context.module_inputs] 66 | ), 67 | ) 68 | 69 | objs = [] 70 | for impl_src in ctx.files.impl_srcs: 71 | objs += cc_module_compile_action(ctx, src=impl_src, 72 | compilation_context=compilation_context, 73 | module_info=module_info) 74 | 75 | return objs, CcInfo(linking_context=cc_info_impl_deps.linking_context) 76 | 77 | def _cc_module_impl(ctx): 78 | if ctx.attr.is_system and ctx.file.src: 79 | fail("src must not be specified if is_system is True") 80 | if not ctx.file.src and not ctx.attr.is_system: 81 | fail("src must be specified") 82 | 83 | module_name = ctx.label.name 84 | archive_out_file = ctx.actions.declare_file(module_name + ".a") 85 | module_out_file = ctx.actions.declare_file(module_name + ".pcm") 86 | deps = ctx.attr.deps 87 | cc_info_deps = get_cc_info_deps(deps) 88 | module_deps = get_module_deps(deps) 89 | module_info = ModuleCompileInfo( 90 | module_name = module_name, 91 | module_file = module_out_file, 92 | module_dependencies = module_deps, 93 | ) 94 | 95 | module_map = make_module_mapper( 96 | ctx.label.name, 97 | ctx.actions, 98 | module_deps) 99 | compilation_context = make_module_compilation_context(cc_info_deps, module_map, module_deps) 100 | objs = [] 101 | objs += cc_module_compile_action(ctx, src=ctx.file.src, 102 | compilation_context=compilation_context, 103 | module_info=module_info, is_interface=True, 104 | is_system=ctx.attr.is_system) 105 | 106 | impl_objs, impl_cc_info = compile_module_impl_srcs(ctx, module_name, module_out_file) 107 | objs += impl_objs 108 | 109 | linking_context = cc_module_archive_action(ctx, objs, archive_out_file) 110 | outputs = [ 111 | archive_out_file, 112 | module_out_file, 113 | ] 114 | cc_info = CcInfo( 115 | compilation_context = cc_info_deps.compilation_context, 116 | linking_context = linking_context 117 | ) 118 | cc_info = cc_common.merge_cc_infos(cc_infos=[cc_info, impl_cc_info, cc_info_deps]) 119 | dep_files = outputs 120 | if ctx.file.src: 121 | dep_files += [ctx.file.src] 122 | return [ 123 | DefaultInfo(files = depset(dep_files)), 124 | cc_info, 125 | module_info, 126 | ] 127 | 128 | _cc_module_attrs = { 129 | "src": attr.label(mandatory = False, allow_single_file = True), 130 | "impl_srcs": attr.label_list(mandatory=False, allow_files=True), 131 | "impl_deps": attr.label_list(), 132 | "is_system": attr.bool(default=False), 133 | } 134 | 135 | cc_module = rule( 136 | implementation = _cc_module_impl, 137 | attrs = dict(_common_attrs.items() + _cc_module_attrs.items()), 138 | toolchains = ["@bazel_tools//tools/cpp:toolchain_type"], 139 | incompatible_use_toolchain_transition = True, 140 | fragments = ["cpp"], 141 | ) 142 | 143 | ########################################################################################### 144 | # cc_header_module 145 | ########################################################################################### 146 | def _cc_header_module_impl(ctx): 147 | hdr = ctx.file.hdr 148 | 149 | module_name = "./" + hdr.path 150 | module_out_file = ctx.actions.declare_file(hdr.basename + ".gcm") 151 | 152 | includes = [] 153 | hdr_dep = [hdr] 154 | outputs = [module_out_file] 155 | if ctx.attr.include_path: 156 | gen_dir = module_out_file.dirname + "/" + hdr.basename + "-include" 157 | inc_dir = hdr.basename + "-include/" + ctx.attr.include_path 158 | hdr_mirror = ctx.actions.declare_file(inc_dir + "/" + hdr.basename) 159 | hdr_dir = ctx.actions.declare_directory(hdr_mirror.dirname) 160 | ctx.actions.run_shell( 161 | outputs = [hdr_dir], 162 | command = "mkdir -p %s" % hdr_dir.path, 163 | ) 164 | ctx.actions.run_shell( 165 | outputs = [hdr_mirror], 166 | inputs = [hdr], 167 | command = "cp %s %s" % (hdr.path, hdr_mirror.path), 168 | ) 169 | includes = [ 170 | gen_dir 171 | ] 172 | hdr_dep = [hdr_mirror] 173 | outputs.append(hdr_mirror) 174 | hdr = hdr_mirror 175 | module_name = "./" + hdr_mirror.path 176 | 177 | 178 | deps = ctx.attr.deps 179 | cc_info_deps = get_cc_info_deps(deps) 180 | 181 | module_deps = get_module_deps(deps) 182 | 183 | module_info = ModuleCompileInfo( 184 | module_name = module_name, 185 | module_file = module_out_file, 186 | module_dependencies = module_deps, 187 | ) 188 | module_map = make_module_mapper( 189 | ctx.label.name, 190 | ctx.actions, 191 | module_deps) 192 | compilation_context = make_module_compilation_context(cc_info_deps, module_map, module_deps) 193 | cc_header_module_compile_action(ctx, src=hdr, 194 | compilation_context=compilation_context, 195 | module_info=module_info) 196 | 197 | hdr_compilation_context = cc_common.create_compilation_context( 198 | headers = depset(hdr_dep), 199 | includes = depset(includes), 200 | ) 201 | cc_info = CcInfo( 202 | compilation_context = hdr_compilation_context, 203 | ) 204 | cc_info = cc_common.merge_cc_infos(cc_infos=[cc_info, cc_info_deps]) 205 | return [ 206 | DefaultInfo(files = depset(outputs)), 207 | cc_info, 208 | module_info, 209 | ] 210 | 211 | 212 | _cc_header_module_attrs = { 213 | "hdr": attr.label(mandatory = True, allow_single_file = True), 214 | "include_path": attr.string(), 215 | } 216 | 217 | cc_header_module = rule( 218 | implementation = _cc_header_module_impl, 219 | attrs = dict(_common_attrs.items() + _cc_header_module_attrs.items()), 220 | toolchains = ["@bazel_tools//tools/cpp:toolchain_type"], 221 | incompatible_use_toolchain_transition = True, 222 | fragments = ["cpp"], 223 | ) 224 | 225 | ########################################################################################### 226 | # cc_module_library 227 | ########################################################################################### 228 | def _cc_module_library_impl(ctx): 229 | archive_out_file = ctx.actions.declare_file(ctx.label.name + ".a") 230 | 231 | deps = ctx.attr.deps 232 | cc_info_deps = get_cc_info_deps(deps) 233 | module_deps = get_module_deps(deps) 234 | 235 | module_map = make_module_mapper(ctx.label.name, ctx.actions, module_deps) 236 | 237 | compilation_context = cc_common.create_compilation_context( 238 | headers = depset(ctx.files.hdrs), 239 | ) 240 | compilation_context = make_module_compilation_context( 241 | cc_common.merge_cc_infos(cc_infos=[ 242 | CcInfo( 243 | compilation_context = compilation_context, 244 | ), 245 | cc_info_deps, 246 | ]), 247 | module_map, module_deps) 248 | 249 | objs = [] 250 | for src in ctx.files.srcs: 251 | objs += cc_module_compile_action(ctx, src=src, 252 | compilation_context = compilation_context) 253 | 254 | linking_context = cc_module_archive_action(ctx, objs, archive_out_file) 255 | outputs = [ 256 | archive_out_file, 257 | ] 258 | cc_info = CcInfo( 259 | compilation_context = compilation_context.compilation_context, 260 | linking_context = linking_context 261 | ) 262 | cc_info = cc_common.merge_cc_infos(cc_infos=[cc_info, cc_info_deps]) 263 | return [ 264 | DefaultInfo(files = depset(outputs)), 265 | cc_info, 266 | ] 267 | 268 | _cc_module_library_attrs = { 269 | "hdrs": attr.label_list(mandatory=False, allow_files=True), 270 | "srcs": attr.label_list(mandatory=False, allow_files=True), 271 | } 272 | 273 | cc_module_library = rule( 274 | implementation = _cc_module_library_impl, 275 | attrs = dict(_common_attrs.items() + _cc_module_library_attrs.items()), 276 | toolchains = ["@bazel_tools//tools/cpp:toolchain_type"], 277 | incompatible_use_toolchain_transition = True, 278 | fragments = ["cpp"], 279 | ) 280 | 281 | ########################################################################################### 282 | # cc_module_binary 283 | ########################################################################################### 284 | def _cc_module_binary_impl(ctx): 285 | exe = ctx.actions.declare_file(ctx.label.name) 286 | deps = ctx.attr.deps 287 | cc_info_deps = get_cc_info_deps(deps) 288 | module_deps = get_module_deps(deps) 289 | 290 | module_map = make_module_mapper(ctx.label.name, ctx.actions, module_deps) 291 | 292 | compilation_context = make_module_compilation_context(cc_info_deps, module_map, module_deps) 293 | 294 | 295 | objs = [] 296 | for src in ctx.files.srcs: 297 | objs += cc_module_compile_action(ctx, src=src, 298 | compilation_context = compilation_context) 299 | cc_module_link_action(ctx, objs, cc_info_deps.linking_context, exe) 300 | 301 | return [ 302 | DefaultInfo(files = depset([exe]), executable=exe), 303 | ] 304 | 305 | _cc_module_binary_attrs = { 306 | "srcs": attr.label_list(mandatory=True, allow_files=True), 307 | } 308 | 309 | cc_module_binary = rule( 310 | implementation = _cc_module_binary_impl, 311 | attrs = dict(_common_attrs.items() + _cc_module_binary_attrs.items()), 312 | toolchains = ["@bazel_tools//tools/cpp:toolchain_type"], 313 | incompatible_use_toolchain_transition = True, 314 | fragments = ["cpp"], 315 | executable = True, 316 | ) 317 | -------------------------------------------------------------------------------- /cc_module/private/cc_module_archive.bzl: -------------------------------------------------------------------------------- 1 | # Copyright 2019 The Bazel Authors. All rights reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | load("@rules_cc//cc:action_names.bzl", "CPP_LINK_STATIC_LIBRARY_ACTION_NAME") 16 | load("@rules_cc//cc:toolchain_utils.bzl", "find_cpp_toolchain") 17 | 18 | def make_linking_context(ctx, cc_toolchain, feature_configuration, output_file): 19 | linker_input = cc_common.create_linker_input( 20 | owner = ctx.label, 21 | libraries = depset(direct = [ 22 | cc_common.create_library_to_link( 23 | actions = ctx.actions, 24 | feature_configuration = feature_configuration, 25 | cc_toolchain = cc_toolchain, 26 | static_library = output_file, 27 | ), 28 | ]), 29 | ) 30 | compilation_context = cc_common.create_compilation_context() 31 | return cc_common.create_linking_context( 32 | linker_inputs = depset(direct = [linker_input])) 33 | 34 | def cc_module_archive_action(ctx, objs, output_file): 35 | cc_toolchain = find_cpp_toolchain(ctx) 36 | 37 | feature_configuration = cc_common.configure_features( 38 | ctx = ctx, 39 | cc_toolchain = cc_toolchain, 40 | requested_features = ctx.features, 41 | unsupported_features = ctx.disabled_features, 42 | ) 43 | 44 | archiver_path = cc_common.get_tool_for_action( 45 | feature_configuration = feature_configuration, 46 | action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME, 47 | ) 48 | 49 | archiver_variables = cc_common.create_link_variables( 50 | feature_configuration = feature_configuration, 51 | cc_toolchain = cc_toolchain, 52 | output_file = output_file.path, 53 | is_using_linker = False, 54 | ) 55 | 56 | command_line = cc_common.get_memory_inefficient_command_line( 57 | feature_configuration = feature_configuration, 58 | action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME, 59 | variables = archiver_variables, 60 | ) 61 | 62 | env = cc_common.get_environment_variables( 63 | feature_configuration = feature_configuration, 64 | action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME, 65 | variables = archiver_variables, 66 | ) 67 | 68 | args = ctx.actions.args() 69 | args.add_all(command_line) 70 | for obj in objs: 71 | args.add(obj) 72 | 73 | ctx.actions.run( 74 | executable = archiver_path, 75 | arguments = [args], 76 | env = env, 77 | inputs = depset( 78 | direct = objs, 79 | transitive = [ 80 | cc_toolchain.all_files, 81 | ], 82 | ), 83 | outputs = [output_file], 84 | ) 85 | 86 | return make_linking_context(ctx, cc_toolchain, feature_configuration, output_file) 87 | -------------------------------------------------------------------------------- /cc_module/private/cc_module_compile.bzl: -------------------------------------------------------------------------------- 1 | # Copyright 2019 The Bazel Authors. All rights reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | load("@rules_cc//cc:action_names.bzl", "CPP_COMPILE_ACTION_NAME") 16 | load("@rules_cc//cc:toolchain_utils.bzl", "find_cpp_toolchain") 17 | load("//cc_module/private:provider.bzl", "ModuleCompileInfo") 18 | 19 | 20 | def replace_extension(f, new_ext): 21 | ext = f.extension 22 | return f.basename[:-len(ext)] + new_ext 23 | 24 | def make_source(ctx, src, module_info): 25 | src = ctx.actions.declare_file('%s.cc' % module_info.module_name) 26 | cmd = "touch %s" % src.path 27 | ctx.actions.run_shell( 28 | outputs = [src], 29 | command = cmd, 30 | ) 31 | return src 32 | 33 | def cc_module_compile_action(ctx, src, compilation_context, module_info=None, is_interface=False, is_system=False): 34 | cc_toolchain = find_cpp_toolchain(ctx) 35 | 36 | if not src: 37 | src = make_source(ctx, src, module_info) 38 | obj_name = replace_extension(src, "o") 39 | if is_interface: 40 | obj_name = "cc_module_interface-" + obj_name 41 | obj = ctx.actions.declare_file(obj_name) 42 | 43 | feature_configuration = cc_common.configure_features( 44 | ctx = ctx, 45 | cc_toolchain = cc_toolchain, 46 | requested_features = ctx.features, 47 | unsupported_features = ctx.disabled_features, 48 | ) 49 | c_compiler_path = cc_common.get_tool_for_action( 50 | feature_configuration = feature_configuration, 51 | action_name = CPP_COMPILE_ACTION_NAME, 52 | ) 53 | cc = compilation_context.compilation_context 54 | copts = getattr(ctx.attr, "copts", []) 55 | c_compile_variables = cc_common.create_compile_variables( 56 | feature_configuration = feature_configuration, 57 | cc_toolchain = cc_toolchain, 58 | user_compile_flags = ctx.fragments.cpp.copts + ctx.fragments.cpp.conlyopts + copts, 59 | include_directories = cc.includes, 60 | quote_include_directories = cc.quote_includes, 61 | system_include_directories = cc.system_includes, 62 | source_file = src.path, 63 | ) 64 | command_line = cc_common.get_memory_inefficient_command_line( 65 | feature_configuration = feature_configuration, 66 | action_name = CPP_COMPILE_ACTION_NAME, 67 | variables = c_compile_variables, 68 | ) 69 | command_line = list(command_line) 70 | command_line += ["-iquote", "."] 71 | 72 | env = cc_common.get_environment_variables( 73 | feature_configuration = feature_configuration, 74 | action_name = CPP_COMPILE_ACTION_NAME, 75 | variables = c_compile_variables, 76 | ) 77 | 78 | driver_args = [] 79 | driver_args += ['--object_out', obj.path] 80 | driver_args += ['--module_map', compilation_context.module_mapper.path] 81 | if is_system: 82 | driver_args += ['--is_system'] 83 | if module_info: 84 | driver_args += ['--module_name', module_info.module_name] 85 | driver_args += ['--module_file', module_info.module_file.path] 86 | 87 | outputs = [obj] 88 | if is_interface: 89 | outputs.append(module_info.module_file) 90 | driver_args += ['--module_interface'] 91 | 92 | 93 | ctx.actions.run( 94 | executable = ctx.executable._driver, 95 | arguments = driver_args + ["--", c_compiler_path] + command_line, 96 | env = env, 97 | inputs = depset( 98 | [src], 99 | transitive = [compilation_context.module_inputs, cc_toolchain.all_files], 100 | ), 101 | outputs = outputs, 102 | ) 103 | return [obj] 104 | 105 | def cc_header_module_compile_action(ctx, src, compilation_context, module_info): 106 | cc_toolchain = find_cpp_toolchain(ctx) 107 | 108 | feature_configuration = cc_common.configure_features( 109 | ctx = ctx, 110 | cc_toolchain = cc_toolchain, 111 | requested_features = ctx.features, 112 | unsupported_features = ctx.disabled_features, 113 | ) 114 | c_compiler_path = cc_common.get_tool_for_action( 115 | feature_configuration = feature_configuration, 116 | action_name = CPP_COMPILE_ACTION_NAME, 117 | ) 118 | cc = compilation_context.compilation_context 119 | c_compile_variables = cc_common.create_compile_variables( 120 | feature_configuration = feature_configuration, 121 | cc_toolchain = cc_toolchain, 122 | user_compile_flags = ctx.fragments.cpp.copts + ctx.fragments.cpp.conlyopts + \ 123 | ["-x", "c++-header", "-fmodules", "-std=c++20"], 124 | include_directories = cc.includes, 125 | quote_include_directories = cc.quote_includes, 126 | system_include_directories = cc.system_includes, 127 | source_file = src.path, 128 | ) 129 | command_line = cc_common.get_memory_inefficient_command_line( 130 | feature_configuration = feature_configuration, 131 | action_name = CPP_COMPILE_ACTION_NAME, 132 | variables = c_compile_variables, 133 | ) 134 | command_line = list(command_line) 135 | command_line += ["-iquote", "."] 136 | 137 | env = cc_common.get_environment_variables( 138 | feature_configuration = feature_configuration, 139 | action_name = CPP_COMPILE_ACTION_NAME, 140 | variables = c_compile_variables, 141 | ) 142 | 143 | driver_args = [] 144 | driver_args += ['--module_map', compilation_context.module_mapper.path] 145 | 146 | outputs = [module_info.module_file] 147 | driver_args += ['--module_name', module_info.module_name] 148 | driver_args += ['--module_file', module_info.module_file.path] 149 | 150 | ctx.actions.run( 151 | executable = ctx.executable._driver, 152 | arguments = driver_args + ["--", c_compiler_path] + command_line, 153 | env = env, 154 | inputs = depset( 155 | [src], 156 | transitive = [compilation_context.module_inputs, cc_toolchain.all_files], 157 | ), 158 | outputs = outputs, 159 | ) 160 | -------------------------------------------------------------------------------- /cc_module/private/cc_module_link.bzl: -------------------------------------------------------------------------------- 1 | # Copyright 2019 The Bazel Authors. All rights reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | load("@rules_cc//cc:action_names.bzl", "CPP_LINK_EXECUTABLE_ACTION_NAME") 16 | load("@rules_cc//cc:toolchain_utils.bzl", "find_cpp_toolchain") 17 | 18 | def get_linker_and_args(ctx, cc_toolchain, feature_configuration, rpaths, output_file): 19 | user_link_flags = ctx.fragments.cpp.linkopts 20 | link_variables = cc_common.create_link_variables( 21 | feature_configuration = feature_configuration, 22 | cc_toolchain = cc_toolchain, 23 | is_linking_dynamic_library = False, 24 | runtime_library_search_directories = rpaths, 25 | user_link_flags = user_link_flags, 26 | output_file = output_file, 27 | ) 28 | link_args = cc_common.get_memory_inefficient_command_line( 29 | feature_configuration = feature_configuration, 30 | action_name = CPP_LINK_EXECUTABLE_ACTION_NAME, 31 | variables = link_variables, 32 | ) 33 | link_env = cc_common.get_environment_variables( 34 | feature_configuration = feature_configuration, 35 | action_name = CPP_LINK_EXECUTABLE_ACTION_NAME, 36 | variables = link_variables, 37 | ) 38 | ld = cc_common.get_tool_for_action( 39 | feature_configuration = feature_configuration, 40 | action_name = CPP_LINK_EXECUTABLE_ACTION_NAME, 41 | ) 42 | 43 | return ld, link_args, link_env 44 | 45 | def cc_module_link_action(ctx, objs, linking_context, exe): 46 | cc_toolchain = find_cpp_toolchain(ctx) 47 | 48 | feature_configuration = cc_common.configure_features( 49 | ctx = ctx, 50 | cc_toolchain = cc_toolchain, 51 | requested_features = ctx.features, 52 | unsupported_features = ctx.disabled_features, 53 | ) 54 | 55 | compilation_outputs = cc_common.create_compilation_outputs( 56 | objects = depset(objs), 57 | ) 58 | 59 | ld, link_args, link_env = get_linker_and_args(ctx, cc_toolchain, feature_configuration, 60 | depset(), exe.path) 61 | 62 | linkopts = getattr(ctx.attr, "linkopts", []) 63 | 64 | args = ctx.actions.args() 65 | args.add_all(link_args) 66 | args.add_all(linkopts) 67 | args.add_all(objs) 68 | 69 | inputs = [] 70 | for linker_inputs in linking_context.linker_inputs.to_list(): 71 | for lib in linker_inputs.libraries: 72 | if lib.static_library: 73 | args.add(lib.static_library) 74 | inputs.append(lib.static_library) 75 | if lib.objects: 76 | args.add(lib.objects) 77 | inputs += lib.objects 78 | args.add_all(linker_inputs.user_link_flags) 79 | 80 | ctx.actions.run( 81 | executable = ld, 82 | arguments = [args], 83 | env = link_env, 84 | use_default_shell_env = True, 85 | inputs = depset( 86 | direct = objs + inputs, 87 | transitive = [cc_toolchain.all_files], 88 | ), 89 | outputs = [exe], 90 | ) 91 | -------------------------------------------------------------------------------- /cc_module/private/provider.bzl: -------------------------------------------------------------------------------- 1 | ModuleCompileInfo = provider(doc = "", fields = [ 2 | "module_name", 3 | "module_file", 4 | "module_dependencies", 5 | ]) 6 | 7 | ModuleCompilationContext = provider( 8 | doc = "", 9 | fields = [ 10 | "compilation_context", 11 | "module_mapper", 12 | "module_inputs", 13 | ], 14 | ) 15 | -------------------------------------------------------------------------------- /cc_module/private/utility.bzl: -------------------------------------------------------------------------------- 1 | load("//cc_module/private:provider.bzl", "ModuleCompilationContext", "ModuleCompileInfo") 2 | 3 | def get_cc_info_deps(deps): 4 | return cc_common.merge_cc_infos( 5 | cc_infos = [dep[CcInfo] for dep in deps]) 6 | 7 | def get_module_deps(deps): 8 | direct = [] 9 | transitive = [] 10 | for dep in deps: 11 | if not ModuleCompileInfo in dep: 12 | continue 13 | module = dep[ModuleCompileInfo] 14 | direct.append(module) 15 | transitive.append(module.module_dependencies) 16 | return depset(direct=direct, transitive=transitive) 17 | 18 | def get_includes(ctx): 19 | includes = ctx.attr.includes 20 | result = [] 21 | basepath = ctx.build_file_path.split("/") 22 | basepath[:-1] 23 | for inc in includes: 24 | result.append("/".join(basepath + [inc])) 25 | return result 26 | 27 | def make_module_mapper(owner, actions, modules): 28 | module_map = "" 29 | for module in modules.to_list(): 30 | module_map += "%s %s\n" % (module.module_name, module.module_file.path) 31 | map_file = actions.declare_file(owner + "-module-map") 32 | actions.write(map_file, module_map) 33 | return map_file 34 | 35 | 36 | def make_module_compilation_context(cc_info_deps, mapper, module_deps): 37 | module_files = [m.module_file for m in module_deps.to_list()] 38 | return ModuleCompilationContext( 39 | compilation_context = cc_info_deps.compilation_context, 40 | module_mapper = mapper, 41 | module_inputs = depset( 42 | direct = [mapper] + module_files, 43 | transitive = [cc_info_deps.compilation_context.headers], 44 | ) 45 | ) 46 | 47 | def get_header_module_name(hdr, include_path): 48 | if not include_path: 49 | return "./" + hdr.path 50 | return include_path + "/" + hdr.basename 51 | 52 | 53 | -------------------------------------------------------------------------------- /ci/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:21.04 2 | 3 | WORKDIR /third_party 4 | 5 | ADD setup_build_environment.sh /third_party 6 | ADD install_bazel.sh /third_party 7 | ADD install_clang.sh /third_party 8 | 9 | RUN /third_party/setup_build_environment.sh \ 10 | && /third_party/install_clang.sh \ 11 | && /third_party/install_bazel.sh 12 | 13 | EXPOSE 8888 14 | -------------------------------------------------------------------------------- /ci/install_bazel.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | BAZELISK_VERSION=v1.1.0 6 | 7 | wget -O /usr/local/bin/bazel https://github.com/bazelbuild/bazelisk/releases/download/$BAZELISK_VERSION/bazelisk-linux-amd64 8 | chmod +x /usr/local/bin/bazel 9 | -------------------------------------------------------------------------------- /ci/install_clang.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | apt-get update 5 | export DEBIAN_FRONTEND=noninteractive 6 | export TZ=Etc/UTC 7 | apt-get install --no-install-recommends --no-install-suggests -y \ 8 | ca-certificates \ 9 | gnupg \ 10 | software-properties-common \ 11 | wget 12 | 13 | wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add - 14 | apt-add-repository -y "deb http://apt.llvm.org/hirsute/ llvm-toolchain-hirsute main" 15 | 16 | apt-get update 17 | apt-get install --no-install-recommends --no-install-suggests -y \ 18 | clang-15 libc++-15-dev libc++abi-15-dev 19 | 20 | cat << EOF > $HOME/.bazelrc 21 | build --action_env CC=/usr/bin/clang-15 22 | build --action_env CXX=/usr/bin/clang++-15 23 | EOF 24 | -------------------------------------------------------------------------------- /ci/run_docker.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | BUILD_IMAGE=bazel-modules 6 | docker image inspect "$BUILD_IMAGE" &> /dev/null || { 7 | docker build -t "$BUILD_IMAGE" ci 8 | } 9 | 10 | if [ -n "$1" ]; then 11 | docker run -v "$PWD":/src -w /src -it "$BUILD_IMAGE" /bin/bash -lc "$1" 12 | else 13 | docker run -v "$PWD":/src -w /src --privileged -it "$BUILD_IMAGE" /bin/bash -l 14 | fi 15 | 16 | -------------------------------------------------------------------------------- /ci/setup_build_environment.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | export DEBIAN_FRONTEND="noninteractive" 5 | export TZ=Etc/UTC 6 | apt-get update 7 | apt-get install --no-install-recommends --no-install-suggests -y \ 8 | software-properties-common \ 9 | build-essential \ 10 | zip \ 11 | git \ 12 | ca-certificates \ 13 | curl \ 14 | gnupg2 \ 15 | ssh \ 16 | vim \ 17 | wget \ 18 | python 19 | -------------------------------------------------------------------------------- /example/hello-world/BUILD: -------------------------------------------------------------------------------- 1 | load("//cc_module:defs.bzl", "cc_module", "cc_module_binary") 2 | 3 | cc_module( 4 | name = "hello", 5 | src = "hello.ixx", 6 | copts = [ 7 | "-fmodules", 8 | "-fbuiltin-module-map", 9 | "-stdlib=libc++", 10 | "-std=c++20", 11 | ], 12 | deps = [ 13 | ":_Builtin_stddef_max_align_t", 14 | ":std", 15 | ], 16 | ) 17 | 18 | cc_module_binary( 19 | name = "a.out", 20 | srcs = [ 21 | "main.cc", 22 | ], 23 | deps = [ 24 | ":hello", 25 | ":_Builtin_stddef_max_align_t", 26 | ":std", 27 | ], 28 | copts = [ 29 | "-fmodules", 30 | "-fbuiltin-module-map", 31 | "-stdlib=libc++", 32 | "-std=c++20", 33 | ], 34 | linkopts = [ 35 | "-stdlib=libc++", 36 | ], 37 | ) 38 | 39 | cc_module( 40 | name = "_Builtin_stddef_max_align_t", 41 | is_system = True, 42 | copts = [ 43 | "-fmodules", 44 | "-fbuiltin-module-map", 45 | "-stdlib=libc++", 46 | "-std=c++20", 47 | ], 48 | ) 49 | 50 | cc_module( 51 | name = "std_config", 52 | is_system = True, 53 | copts = [ 54 | "-fmodules", 55 | "-fbuiltin-module-map", 56 | "-stdlib=libc++", 57 | "-std=c++20", 58 | ], 59 | deps = [ 60 | ":_Builtin_stddef_max_align_t", 61 | ], 62 | ) 63 | 64 | cc_module( 65 | name = "std", 66 | is_system = True, 67 | copts = [ 68 | "-fmodules", 69 | "-fbuiltin-module-map", 70 | "-stdlib=libc++", 71 | "-std=c++20", 72 | ], 73 | deps = [ 74 | ":_Builtin_stddef_max_align_t", 75 | ":std_config", 76 | ], 77 | ) 78 | -------------------------------------------------------------------------------- /example/hello-world/hello.ixx: -------------------------------------------------------------------------------- 1 | // hello.ixx 2 | export module hello; 3 | 4 | import ; 5 | import ; 6 | 7 | // the module purview starts here 8 | // provide a function to users by exporting it 9 | export inline void say_hello(std::string_view const &name) 10 | { 11 | std::cout << "Hello " << name << "!\n"; 12 | } 13 | -------------------------------------------------------------------------------- /example/hello-world/main.cc: -------------------------------------------------------------------------------- 1 | // main.cc 2 | import hello; 3 | 4 | import ; 5 | 6 | int main() { 7 | say_hello("world"); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /example/module-library/BUILD: -------------------------------------------------------------------------------- 1 | load("@bazel_skylib//:bzl_library.bzl", "bzl_library") 2 | load("//cc_module:defs.bzl", "cc_module", "cc_module_library") 3 | 4 | package(default_visibility = ["//visibility:public"]) 5 | 6 | licenses(["notice"]) 7 | 8 | cc_module( 9 | name = "a", 10 | src = "a.cc", 11 | copts = [ 12 | "-fmodules-ts", 13 | "-std=c++20", 14 | ], 15 | ) 16 | 17 | cc_module_library( 18 | name = "b", 19 | hdrs = [ 20 | "b.h", 21 | ], 22 | srcs = [ 23 | "b.cc", 24 | ], 25 | deps = [ 26 | ":a", 27 | ], 28 | copts = [ 29 | "-fmodules-ts", 30 | "-std=c++20", 31 | ], 32 | ) 33 | 34 | cc_binary( 35 | name = "a.out", 36 | srcs = [ 37 | "main.cc", 38 | ], 39 | deps = [ 40 | ":b", 41 | ], 42 | copts = [ 43 | "-fmodules-ts", 44 | "-std=c++20", 45 | ], 46 | ) 47 | -------------------------------------------------------------------------------- /example/module-library/a.cc: -------------------------------------------------------------------------------- 1 | export module a; 2 | 3 | export template 4 | T plus(T a, T b) { 5 | return a + b; 6 | } 7 | -------------------------------------------------------------------------------- /example/module-library/b.cc: -------------------------------------------------------------------------------- 1 | #include "example/module-library/b.h" 2 | 3 | import a; 4 | 5 | 6 | double plus_float(double x, double y) { 7 | return plus(x, y); 8 | } 9 | -------------------------------------------------------------------------------- /example/module-library/b.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | double plus_float(double x, double y); 4 | -------------------------------------------------------------------------------- /example/module-library/main.cc: -------------------------------------------------------------------------------- 1 | #include "example/module-library/b.h" 2 | 3 | #include 4 | 5 | int main() { 6 | std::cout << plus_float(1.0, 2.0) << std::endl; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /example/multi_src_module/BUILD: -------------------------------------------------------------------------------- 1 | load("@bazel_skylib//:bzl_library.bzl", "bzl_library") 2 | load("//cc_module:defs.bzl", "cc_module", "cc_module_binary") 3 | 4 | package(default_visibility = ["//visibility:public"]) 5 | 6 | licenses(["notice"]) 7 | 8 | cc_module( 9 | name = "spanish_english_dictionary", 10 | src = "spanish_english_dictionary.cc", 11 | impl_srcs = [ 12 | "spanish_english_dictionary_impl.cc", 13 | ], 14 | copts = [ 15 | "-fmodules", 16 | "-fbuiltin-module-map", 17 | "-stdlib=libc++", 18 | "-std=c++20", 19 | ], 20 | deps = [ 21 | ":_Builtin_stddef_max_align_t", 22 | ":std", 23 | ], 24 | ) 25 | 26 | cc_module( 27 | name = "speech", 28 | src = "speech.cc", 29 | impl_srcs = [ 30 | "speech_impl.cc", 31 | ], 32 | copts = [ 33 | "-fmodules", 34 | "-fbuiltin-module-map", 35 | "-fno-implicit-modules", 36 | "-stdlib=libc++", 37 | "-std=c++20", 38 | ], 39 | impl_deps = [ 40 | ":spanish_english_dictionary", 41 | ], 42 | deps = [ 43 | ":_Builtin_stddef_max_align_t", 44 | ":std", 45 | ], 46 | ) 47 | 48 | cc_module_binary( 49 | name = "a.out", 50 | srcs = [ 51 | "main.cc", 52 | ], 53 | copts = [ 54 | "-fmodules", 55 | "-fbuiltin-module-map", 56 | "-fno-implicit-modules", 57 | "-stdlib=libc++", 58 | "-std=c++20", 59 | ], 60 | deps = [ 61 | ":speech", 62 | ], 63 | linkopts = [ 64 | "-stdlib=libc++", 65 | ], 66 | ) 67 | 68 | cc_module( 69 | name = "_Builtin_stddef_max_align_t", 70 | is_system = True, 71 | copts = [ 72 | "-fmodules", 73 | "-fbuiltin-module-map", 74 | "-stdlib=libc++", 75 | "-std=c++20", 76 | ], 77 | ) 78 | 79 | cc_module( 80 | name = "std", 81 | is_system = True, 82 | copts = [ 83 | "-fmodules", 84 | "-fbuiltin-module-map", 85 | "-stdlib=libc++", 86 | "-std=c++20", 87 | ], 88 | deps = [ 89 | ":_Builtin_stddef_max_align_t", 90 | ], 91 | ) 92 | -------------------------------------------------------------------------------- /example/multi_src_module/main.cc: -------------------------------------------------------------------------------- 1 | import speech; 2 | 3 | import ; 4 | import ; 5 | 6 | std::string get_phrase() { 7 | return "Hello, world!"; 8 | } 9 | 10 | int main() { 11 | std::cout << get_phrase() << std::endl; 12 | std::cout << get_phrase_en() << std::endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /example/multi_src_module/spanish_english_dictionary.cc: -------------------------------------------------------------------------------- 1 | export module spanish_english_dictionary; 2 | 3 | import ; 4 | import ; 5 | 6 | export std::string translate(std::string_view s); 7 | -------------------------------------------------------------------------------- /example/multi_src_module/spanish_english_dictionary_impl.cc: -------------------------------------------------------------------------------- 1 | module spanish_english_dictionary; 2 | 3 | import ; 4 | import ; 5 | 6 | std::string translate(std::string_view s) { 7 | if (s == "Hello") { 8 | return "Hola"; 9 | } 10 | return "???"; 11 | } 12 | -------------------------------------------------------------------------------- /example/multi_src_module/speech.cc: -------------------------------------------------------------------------------- 1 | export module speech; 2 | 3 | import ; 4 | 5 | export std::string get_phrase_en(); 6 | export std::string get_phrase_es(); 7 | -------------------------------------------------------------------------------- /example/multi_src_module/speech_impl.cc: -------------------------------------------------------------------------------- 1 | module speech; 2 | 3 | import ; 4 | 5 | import spanish_english_dictionary; 6 | 7 | std::string get_phrase_en() { 8 | return "Hello, world!"; 9 | } 10 | 11 | std::string get_phrase_es() { return translate("Hello"); } 12 | -------------------------------------------------------------------------------- /example/template-module/BUILD: -------------------------------------------------------------------------------- 1 | 2 | load("@bazel_skylib//:bzl_library.bzl", "bzl_library") 3 | load("//cc_module:defs.bzl", "cc_module", "cc_module_binary") 4 | 5 | package(default_visibility = ["//visibility:public"]) 6 | 7 | licenses(["notice"]) 8 | 9 | cc_module( 10 | name = "algorithm", 11 | src = "algorithm.cc", 12 | copts = [ 13 | "-fmodules", 14 | "-fbuiltin-module-map", 15 | "-stdlib=libc++", 16 | "-std=c++20", 17 | ], 18 | deps = [ 19 | ":_Builtin_stddef_max_align_t", 20 | ":std", 21 | ], 22 | ) 23 | 24 | cc_module_binary( 25 | name = "a.out", 26 | srcs = [ 27 | "main.cc", 28 | ], 29 | copts = [ 30 | "-fmodules", 31 | "-fbuiltin-module-map", 32 | "-stdlib=libc++", 33 | "-std=c++20", 34 | ], 35 | deps = [ 36 | ":algorithm", 37 | ], 38 | linkopts = [ 39 | "-stdlib=libc++", 40 | ], 41 | ) 42 | 43 | cc_module( 44 | name = "_Builtin_stddef_max_align_t", 45 | is_system = True, 46 | copts = [ 47 | "-fmodules", 48 | "-fbuiltin-module-map", 49 | "-stdlib=libc++", 50 | "-std=c++20", 51 | ], 52 | ) 53 | 54 | cc_module( 55 | name = "std_config", 56 | is_system = True, 57 | copts = [ 58 | "-fmodules", 59 | "-fbuiltin-module-map", 60 | "-stdlib=libc++", 61 | "-std=c++20", 62 | ], 63 | deps = [ 64 | ":_Builtin_stddef_max_align_t", 65 | ], 66 | ) 67 | 68 | cc_module( 69 | name = "std", 70 | is_system = True, 71 | copts = [ 72 | "-fmodules", 73 | "-fbuiltin-module-map", 74 | "-stdlib=libc++", 75 | "-std=c++20", 76 | ], 77 | deps = [ 78 | ":_Builtin_stddef_max_align_t", 79 | ":std_config", 80 | ], 81 | ) 82 | -------------------------------------------------------------------------------- /example/template-module/algorithm.cc: -------------------------------------------------------------------------------- 1 | module; 2 | 3 | #include 4 | 5 | export module algorithm; 6 | 7 | import ; 8 | import ; 9 | import ; 10 | 11 | export template 12 | auto compute_median(Iter first, Iter last) { 13 | using T = std::decay_t; 14 | auto n = std::distance(first, last); 15 | assert(n > 0); 16 | std::vector v(n); 17 | std::transform(first, last, v.begin(), [](auto& x) { return &x; }); 18 | auto mid = v.begin() + n / 2; 19 | std::nth_element(v.begin(), mid, v.end(), 20 | [](const T* lhs, const T* rhs) { return *lhs < *rhs; }); 21 | return **mid; 22 | } 23 | -------------------------------------------------------------------------------- /example/template-module/main.cc: -------------------------------------------------------------------------------- 1 | import ; 2 | import ; 3 | 4 | import algorithm; 5 | 6 | int main() { 7 | std::vector v1 = {1, -9, 7}; 8 | std::cout << compute_median(v1.begin(), v1.end()) << std::endl; 9 | std::vector v2 = {10.0, 5.0, 7.2, -3.5}; 10 | std::cout << compute_median(v2.begin(), v2.end()) << std::endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /example/transitive/BUILD: -------------------------------------------------------------------------------- 1 | load("@bazel_skylib//:bzl_library.bzl", "bzl_library") 2 | load("//cc_module:defs.bzl", "cc_module", "cc_module_binary") 3 | 4 | package(default_visibility = ["//visibility:public"]) 5 | 6 | licenses(["notice"]) 7 | 8 | cc_module( 9 | name = "a", 10 | src = "a.cc", 11 | copts = [ 12 | "-fmodules", 13 | "-fbuiltin-module-map", 14 | "-stdlib=libc++", 15 | "-std=c++20", 16 | ], 17 | deps = [ 18 | ":b", 19 | ":_Builtin_stddef_max_align_t", 20 | ":std", 21 | ], 22 | ) 23 | 24 | cc_module( 25 | name = "b", 26 | src = "b.cc", 27 | copts = [ 28 | "-fmodules", 29 | "-fbuiltin-module-map", 30 | "-stdlib=libc++", 31 | "-std=c++20", 32 | ], 33 | deps = [ 34 | ":_Builtin_stddef_max_align_t", 35 | ":std", 36 | ], 37 | ) 38 | 39 | cc_module_binary( 40 | name = "a.out", 41 | srcs = [ 42 | "main.cc", 43 | ], 44 | copts = [ 45 | "-fmodules", 46 | "-fbuiltin-module-map", 47 | "-stdlib=libc++", 48 | "-std=c++20", 49 | ], 50 | deps = [ 51 | ":a", 52 | ], 53 | linkopts = [ 54 | "-stdlib=libc++", 55 | ], 56 | ) 57 | 58 | cc_module( 59 | name = "_Builtin_stddef_max_align_t", 60 | is_system = True, 61 | copts = [ 62 | "-fmodules", 63 | "-fbuiltin-module-map", 64 | "-stdlib=libc++", 65 | "-std=c++20", 66 | ], 67 | ) 68 | 69 | cc_module( 70 | name = "std_config", 71 | is_system = True, 72 | copts = [ 73 | "-fmodules", 74 | "-fbuiltin-module-map", 75 | "-stdlib=libc++", 76 | "-std=c++20", 77 | ], 78 | deps = [ 79 | ":_Builtin_stddef_max_align_t", 80 | ], 81 | ) 82 | 83 | cc_module( 84 | name = "std", 85 | is_system = True, 86 | copts = [ 87 | "-fmodules", 88 | "-fbuiltin-module-map", 89 | "-stdlib=libc++", 90 | "-std=c++20", 91 | ], 92 | deps = [ 93 | ":_Builtin_stddef_max_align_t", 94 | ":std_config", 95 | ], 96 | ) 97 | -------------------------------------------------------------------------------- /example/transitive/a.cc: -------------------------------------------------------------------------------- 1 | export module a; 2 | 3 | import ; 4 | 5 | import b; 6 | 7 | export inline void run_a() { 8 | std::cout << "A\n"; 9 | run_b(); 10 | } 11 | -------------------------------------------------------------------------------- /example/transitive/b.cc: -------------------------------------------------------------------------------- 1 | export module b; 2 | 3 | import ; 4 | 5 | export inline void run_b() { 6 | std::cout << "B\n"; 7 | } 8 | -------------------------------------------------------------------------------- /example/transitive/main.cc: -------------------------------------------------------------------------------- 1 | import a; 2 | 3 | int main() { 4 | run_a(); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /util/driver/BUILD: -------------------------------------------------------------------------------- 1 | py_binary( 2 | name = "driver", 3 | srcs = [ 4 | "driver.py", 5 | "clang.py", 6 | "gcc.py", 7 | ], 8 | python_version = "PY3", 9 | visibility = ["//visibility:public"], 10 | ) 11 | -------------------------------------------------------------------------------- /util/driver/__pycache__/clang.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rnburn/rules_cc_module/598d87a2ceb2f515a2f58e505f716c596db03912/util/driver/__pycache__/clang.cpython-36.pyc -------------------------------------------------------------------------------- /util/driver/__pycache__/gcc.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rnburn/rules_cc_module/598d87a2ceb2f515a2f58e505f716c596db03912/util/driver/__pycache__/gcc.cpython-36.pyc -------------------------------------------------------------------------------- /util/driver/clang.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | 4 | import subprocess 5 | 6 | def setup_module_map(driver_args): 7 | m = "" 8 | with open(driver_args.module_map, 'r') as f: 9 | for line in f.readlines(): 10 | name, module_file = line.split(' ') 11 | m += '-fmodule-file=%s=%s' % (name, module_file) 12 | if driver_args.module_file and not driver_args.module_interface: 13 | m += '-fmodule-file=%s\n' % driver_args.module_file 14 | map_file = 'clang-module-map' 15 | with open(map_file, 'w') as f: 16 | f.write(m) 17 | return map_file 18 | 19 | def get_src_type(module_name): 20 | if not module_name: 21 | return 'c++' 22 | if module_name.startswith('.'): 23 | return 'c++-header' 24 | return 'c++-module' 25 | 26 | def make_interface_args(compiler_args): 27 | interface_args = [] 28 | is_source = False 29 | for i in range(len(compiler_args)): 30 | arg = compiler_args[i] 31 | if arg == '-c': 32 | is_source = True 33 | continue 34 | if is_source: 35 | is_source = False 36 | arg = os.readlink(os.getcwd() + "/" + arg) 37 | interface_args.append(arg) 38 | return interface_args 39 | 40 | def make_stub_object(driver_args, compiler): 41 | empty_src = 'empty_src.cc' 42 | open(empty_src, 'a').close() 43 | args = [ 44 | '-o', driver_args.object_out, 45 | '-c', empty_src, 46 | ] 47 | os.execv(compiler, [compiler] + args) 48 | 49 | def query_arguments(driver_args, compiler, interface_args): 50 | additional_args = [ 51 | '-###', 52 | '-x', get_src_type(driver_args.module_name), 53 | '--precompile', 54 | '-o', driver_args.module_file, 55 | ] 56 | res = subprocess.run( 57 | [compiler] + additional_args + interface_args, check=True, capture_output=True) 58 | res = res.stderr.decode('utf-8').splitlines()[-1] 59 | args = res.split() 60 | args_p = [] 61 | args = args[:-1] # drop dummy source 62 | nargs = len(args) 63 | i = 0 64 | while i < nargs: 65 | arg = args[i].strip('"') 66 | if arg == '-emit-module-interface': 67 | arg = '-emit-module' 68 | elif arg.startswith('-fmodule-map-file'): 69 | arg = arg.split('=')[-1] 70 | elif arg == '-D': 71 | i += 2 72 | continue 73 | args_p.append(arg) 74 | i += 1 75 | return args_p 76 | 77 | def make_system_module(driver_args, compiler, interface_args): 78 | args = query_arguments(driver_args, compiler, interface_args) 79 | args.append('-fmodule-name=%s' % driver_args.module_name) 80 | subprocess.run(args, check=True) 81 | 82 | def make_module(driver_args, compiler, interface_args): 83 | additional_args = [ 84 | '-x', get_src_type(driver_args.module_name), 85 | '--precompile', 86 | '-o', driver_args.module_file, 87 | ] 88 | subprocess.run([compiler] + additional_args + interface_args, check=True) 89 | 90 | def invoke_clang(driver_args, compiler, compiler_args): 91 | map_file = setup_module_map(driver_args) 92 | additional_args = [ 93 | '-x', get_src_type(driver_args.module_name), 94 | '--precompile', 95 | '-o', driver_args.module_file, 96 | ] 97 | 98 | compiler_args += ['@' + map_file] 99 | 100 | # compile the module 101 | if driver_args.module_interface: 102 | interface_args = make_interface_args(compiler_args) 103 | if driver_args.is_system: 104 | make_system_module(driver_args, compiler, interface_args) 105 | else: 106 | make_module(driver_args, compiler, interface_args) 107 | 108 | # compile the object file 109 | if driver_args.object_out: 110 | if driver_args.module_interface: 111 | return make_stub_object(driver_args, compiler) 112 | args = [compiler] + compiler_args + ['-o', driver_args.object_out] 113 | os.execv(compiler, [compiler] + compiler_args + ['-o', driver_args.object_out]) 114 | -------------------------------------------------------------------------------- /util/driver/driver.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import argparse 3 | import subprocess 4 | from util.driver.clang import invoke_clang 5 | from util.driver.gcc import invoke_gcc 6 | 7 | argument_parser = argparse.ArgumentParser(prog='driver') 8 | argument_parser.add_argument('--module_map', default=None) 9 | argument_parser.add_argument('--module_interface', action='store_true', default=False) 10 | argument_parser.add_argument('--is_system', action='store_true', default=False) 11 | argument_parser.add_argument('--module_name', default=None) 12 | argument_parser.add_argument('--module_file', default=None) 13 | argument_parser.add_argument('--object_out', default=None) 14 | 15 | def main(): 16 | break_index = sys.argv.index('--') 17 | driver_args = argument_parser.parse_args(sys.argv[1:break_index]) 18 | compiler_command = sys.argv[(break_index+1):] 19 | compiler = compiler_command[0] 20 | compiler_args = compiler_command[1:] 21 | 22 | compiler_version = str(subprocess.check_output([compiler, '-v'], stderr=subprocess.STDOUT)) 23 | if 'clang' in compiler_version: 24 | invoke_clang(driver_args, compiler, compiler_args) 25 | else: 26 | invoke_gcc(driver_args, compiler, compiler_args) 27 | 28 | 29 | if __name__ == "__main__": 30 | main() 31 | -------------------------------------------------------------------------------- /util/driver/gcc.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | def setup_module_map(driver_args): 4 | if not driver_args.module_name: 5 | return driver_args.module_map 6 | m = None 7 | with open(driver_args.module_map, 'r') as f: 8 | m = f.read() 9 | m += "%s %s\n" % (driver_args.module_name, driver_args.module_file) 10 | map_file = 'gcc-module-map' 11 | with open(map_file, 'w') as f: 12 | f.write(m) 13 | return map_file 14 | 15 | def invoke_gcc(driver_args, compiler, compiler_args): 16 | map_file = setup_module_map(driver_args) 17 | compiler_args += ['-fmodule-mapper=%s' % map_file] 18 | if driver_args.object_out: 19 | compiler_args += ['-o', driver_args.object_out] 20 | os.execv(compiler, [compiler] + compiler_args) 21 | --------------------------------------------------------------------------------