Giter VIP home page Giter VIP logo

rules_cc_module's Introduction

rules_cc_module

Rules to use C++20 modules with Bazel.

Getting started

Note: Currently only works with a recent version of clang.

Build a simple module:

cc_module(
    name = "Hello",
    src = "say_hello.cc", # say_hello exports the module Hello
)

# Build a binary with the module
cc_module_binary(
    name = "a.out",
    srcs = [
        "main.cc",  # We can import Hello in main.cc
    ],
    deps = [
        ":Hello",
    ],
)

Build a module with implementation units:

cc_module_binary(
  name = "speech",
  src = "speech.cc",  # speech.cc exports the module speech
  impl_srcs = [
    "speech_impl.cc", # speech_impl.cc provides implements (but doesn't export) speech
  ],
)

Interoperate with regular cc libraries

cc_module(
    name = "a",
    src = "a.cc",
)

cc_module_library(
    name = "b",
    hdrs = [
        "b.h",
    ],
    srcs = [
        "b.cc", # b can import module a, but shouldn't export a module
    ],
    deps = [
        ":a",
    ],
)

# We can use b with regular cc rules
cc_binary(
    name = "a.out",
    srcs = [
        "main.cc",
    ],
    deps = [
        ":b",
    ],
)

Documentation

How to Use C++20 Modules with Bazel

Examples

The directory example demonstrates usage and there is a docker image that provides a build environment. To build the examples, run

./ci/run_docker.sh # spins up a build environment
bazel build //example/... # build the examples

rules_cc_module's People

Contributors

rnburn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

rules_cc_module's Issues

"bazel build //example/..." fails with error: "could not build module 'std_config'"

Hello!
When I do:

git clone https://github.com/rnburn/rules_cc_module
cd rules_cc_module
./ci/run_docker.sh
bazel build //example/...

build fails with error:

root@2add72a9d097:/src# bazel build //example/...
2022/03/20 07:20:11 Downloading https://releases.bazel.build/5.0.0/release/bazel-5.0.0-linux-x86_64...
Extracting Bazel installation...
Starting local Bazel server and connecting to it...
DEBUG: Rule 'bazel_skylib' indicated that a canonical reproducible form can be obtained by modifying arguments shallow_since = "1611945497 -0500"
DEBUG: Repository bazel_skylib instantiated at:
  /src/WORKSPACE:5:15: in <toplevel>
Repository rule git_repository defined at:
  /root/.cache/bazel/_bazel_root/f8087e59fd95af1ae29e8fcb7ff1a3dc/external/bazel_tools/tools/build_defs/repo/git.bzl:199:33: in <toplevel>
INFO: Analyzed 24 targets (44 packages loaded, 463 targets configured).
INFO: Found 24 targets...
ERROR: /src/example/multi_src_module/BUILD:79:10: Action example/multi_src_module/cc_module_interface-std.o failed: (Exit 1): driver failed: error executing command bazel-out/k8-opt-exec-2B5CBBC6/bin/util/driver/driver --object_out bazel-out/k8-fastbuild/bin/example/multi_src_module/cc_module_interface-std.o --module_map ... (remaining 32 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox
error: unable to open output file '/root/.cache/clang/ModuleCache/1VEKDVQVH77C4/std_config-1LRZ8JTHCNMTQ.pcm': 'No such file or directory'
While building module 'std':
In file included from <module-includes>:2:
/usr/lib/llvm-15/bin/../include/c++/v1/ctype.h:32:10: fatal error: could not build module 'std_config'
#include <__config>
 ~~~~~~~~^
2 errors generated.
Traceback (most recent call last):
  File "/root/.cache/bazel/_bazel_root/f8087e59fd95af1ae29e8fcb7ff1a3dc/sandbox/linux-sandbox/26/execroot/com_github_rnburn_bazel_cpp20_modules/bazel-out/k8-opt-exec-2B5CBBC6/bin/util/driver/driver.runfiles/com_github_rnburn_bazel_cpp20_modules/util/driver/driver.py", line 30, in <module>
    main()
  File "/root/.cache/bazel/_bazel_root/f8087e59fd95af1ae29e8fcb7ff1a3dc/sandbox/linux-sandbox/26/execroot/com_github_rnburn_bazel_cpp20_modules/bazel-out/k8-opt-exec-2B5CBBC6/bin/util/driver/driver.runfiles/com_github_rnburn_bazel_cpp20_modules/util/driver/driver.py", line 24, in main
    invoke_clang(driver_args, compiler, compiler_args)
  File "/root/.cache/bazel/_bazel_root/f8087e59fd95af1ae29e8fcb7ff1a3dc/sandbox/linux-sandbox/26/execroot/com_github_rnburn_bazel_cpp20_modules/bazel-out/k8-opt-exec-2B5CBBC6/bin/util/driver/driver.runfiles/com_github_rnburn_bazel_cpp20_modules/util/driver/clang.py", line 104, in invoke_clang
    make_system_module(driver_args, compiler, interface_args)
  File "/root/.cache/bazel/_bazel_root/f8087e59fd95af1ae29e8fcb7ff1a3dc/sandbox/linux-sandbox/26/execroot/com_github_rnburn_bazel_cpp20_modules/bazel-out/k8-opt-exec-2B5CBBC6/bin/util/driver/driver.runfiles/com_github_rnburn_bazel_cpp20_modules/util/driver/clang.py", line 80, in make_system_module
    subprocess.run(args, check=True)
  File "/usr/lib/python3.9/subprocess.py", line 528, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/usr/lib/llvm-15/bin/clang', '-cc1', '-triple', 'x86_64-pc-linux-gnu', '-emit-module', '-disable-free', '-clear-ast-before-backend', '-disable-llvm-verifier', '-discard-value-names', '-main-file-name', 'std.cc', '-mrelocation-model', 'pic', '-pic-level', '2', '-pic-is-pie', '-mframe-pointer=all', '-fmath-errno', '-ffp-contract=on', '-fno-rounding-math', '-mconstructor-aliases', '-funwind-tables=2', '-target-cpu', 'x86-64', '-tune-cpu', 'generic', '-debugger-tuning=gdb', '-fcoverage-compilation-dir=/root/.cache/bazel/_bazel_root/f8087e59fd95af1ae29e8fcb7ff1a3dc/sandbox/linux-sandbox/26/execroot/com_github_rnburn_bazel_cpp20_modules', '-resource-dir', '/usr/lib/llvm-15/lib/clang/15.0.0', '-iquote', '.', '-U', '_FORTIFY_SOURCE', '-internal-isystem', '/usr/lib/llvm-15/bin/../include/c++/v1', '-internal-isystem', '/usr/lib/llvm-15/lib/clang/15.0.0/include', '-internal-isystem', '/usr/local/include', '-internal-isystem', '/usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include', '-internal-externc-isystem', '/usr/include/x86_64-linux-gnu', '-internal-externc-isystem', '/include', '-internal-externc-isystem', '/usr/include', '-Wall', '-Wthread-safety', '-Wself-assign', '-Wunused-but-set-parameter', '-Wno-free-nonheap-object', '-Wno-builtin-macro-redefined', '-std=c++20', '-fdeprecated-macro', '-fdebug-compilation-dir=/root/.cache/bazel/_bazel_root/f8087e59fd95af1ae29e8fcb7ff1a3dc/sandbox/linux-sandbox/26/execroot/com_github_rnburn_bazel_cpp20_modules', '-ferror-limit', '19', '-stack-protector', '1', '-fgnuc-version=4.2.1', '-fmodules', '-fimplicit-module-maps', '-fmodules-cache-path=/root/.cache/clang/ModuleCache', '/usr/lib/llvm-15/lib/clang/15.0.0/include/module.modulemap', '-fmodule-file=_Builtin_stddef_max_align_t=bazel-out/k8-fastbuild/bin/example/multi_src_module/_Builtin_stddef_max_align_t.pcm', '-fmodules-validate-system-headers', '-fcxx-exceptions', '-fexceptions', '-fcolor-diagnostics', '-faddrsig', '-D__GCC_HAVE_DWARF2_CFI_ASM=1', '-o', 'bazel-out/k8-fastbuild/bin/example/multi_src_module/std.pcm', '-x', 'c++', '-fmodule-name=std']' returned non-zero exit status 1.
INFO: Elapsed time: 6.731s, Critical Path: 0.50s
INFO: 78 processes: 47 internal, 31 linux-sandbox.
FAILED: Build did NOT complete successfully

Or as image for better colorized view:
Screenshot from 2022-03-20 11-43-34

What could be the reason? Thanks in advance!

Add support for clang

For clang (reference), to compile a module

// math-m.cc

export module math;

export int add(int a, int b) {
    return a + b;
} 

run

clang++ -std=c++20 - -fmodules-ts -stdlib=libc++ -c math-m.cc -Xclang -emit-module-interface -o math.pcm

when compiling sources that depend on modules (for example, modules A and B) use the -fmodule-file option.

// main.cc
import A;
import B;

...

clang++ -fmodules-ts -std=c++20 -fmodule-file=A=/path/to/a.pcm -fmodule-file=B=/path/to/b.pcm-c main.cc -o main.o

support header unit modules

you should be able to specify a header unit module

cc_header_module(
        name = "a_hdr",
        hdr = "a.h",
        deps = [
              # other cc libraries or other modules
        ],
)

A module should then be able to use a_hdr with

export module b;

import "path/to/a.h"

...

Support shared library binaries

Like cc_binary you should be able to link shared libraries with cc_module_binary. For example,

cc_module_binary(
     name = "liba.so",
     srcs = [
        "a.cc",
     ],
     linkshared = True,
     deps = [
        ":my_module",
     ],
)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.