Skip to content

Conversation

igormcoelho
Copy link
Contributor

This rule allows to direct inject some CMI into the project... like a precompiled .pcm of the std... I managed to use import std; with this trick.

TODO: must update defs.blz as follows:

load("//cc_module/private:cc_module.bzl",
     _cc_module = "cc_module",
     _cc_header_module = "cc_header_module",
     _cc_compiled_module = "cc_compiled_module",
     _cc_module_binary = "cc_module_binary",
     _cc_module_library = "cc_module_library",
)

cc_module = _cc_module
cc_header_module = _cc_header_module
cc_compiled_module = _cc_compiled_module
cc_module_binary = _cc_module_binary
cc_module_library = _cc_module_library

Then, as long as the user precompiles the std, everything works fine. Too bad I couldn't automate the precompilation of std, because the file comes from the system, not from bazel...

I do std.pcm manually on example/hello-world/Makefile:

CXX = clang++-19
CXXFLAGS += -std=c++23 -stdlib=libc++ -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -Wunused-but-set-parameter -Wno-free-nonheap-object -fcolor-diagnostics -fno-omit-frame-pointer '-std=c++23' -fPIC

hello_world: std.pcm hello.pcm main.cc
	$(CXX) $(CXXFLAGS) -fmodule-file=std=std.pcm -fmodule-file=hello=hello.pcm  -o $@ $^

std.pcm: /usr/lib/llvm-19/share/libc++/v1/std.cppm 
	$(CXX) $(CXXFLAGS) -Wno-reserved-identifier -Wno-reserved-module-identifier --precompile -o $@ $^

hello.pcm: hello.cppm std.pcm
	$(CXX) $(CXXFLAGS) -fmodule-file=std=std.pcm -Wno-reserved-identifier -Wno-reserved-module-identifier --precompile -o $@ $^

clean:
	rm -rf *.pcm
	rm -rf hello_world

Then, example is just:

// main.cc
import hello;

//import <string_view>;
import std;

int main() {
  say_hello("world");
  return 0;
}
// hello.cppm
export module hello;

//import <iostream>;
//import <string_view>;
import std;

// the module purview starts here
// provide a function to users by exporting it
export inline void say_hello(std::string_view const &name)
{
  std::cout << "Hello " << name << "!\n";
}

And BUILD for examples/hello-world:

load("//cc_module:defs.bzl", "cc_module", "cc_compiled_module", "cc_module_binary")

cc_module(
    name = "hello",
    src = "hello.cppm",
    copts = [
        "-fmodule-file=std=example/hello-world/std.pcm",
        "-stdlib=libc++",
        "-std=c++23",
    ],
    deps = [":std"],
)


cc_module_binary(
    name = "hello_world",
    srcs = [
        "main.cc",
    ],
    deps = [
        ":hello",
        ":std"
    ],
    copts = [
        "-fmodule-file=std=example/hello-world/std.pcm",
        "-stdlib=libc++",
        "-std=c++23",
    ],
    linkopts = [
        "-stdlib=libc++",
    ],
)

cc_compiled_module(
    name="std",
    cmi="std.pcm"
)

Command will work fine: bazel build //example/hello-world:hello_world

Tested on Bazel 5 up to 8.

On my '.bazelrc` I forced clang compiler:

build --action_env=CC=/usr/bin/clang++-19
build --action_env=CXX=/usr/bin/clang++-19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant