modern-cmake / cppfront

CMake wrapper around the cppfront repository
BSD 3-Clause "New" or "Revised" License
73 stars 19 forks source link

module support for cpp2 files #162

Open adam-ce opened 1 month ago

adam-ce commented 1 month ago

if i understand correctly, this is still something for the future, but to my understanding it's not possible to make libraries with cpp2 files.

e.g., i followed a module example: cmake file:

cmake_minimum_required(VERSION 3.30)

project(cpp2test LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 23)
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi")
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)

FetchContent_Declare(
    cppfront
    GIT_REPOSITORY https://github.com/modern-cmake/cppfront.git
    GIT_TAG main  # or an actual git SHA if you don't like to live dangerously
)

FetchContent_MakeAvailable(cppfront)

add_library(foo)
# Add the module file to the library
target_sources(foo
  PUBLIC
    FILE_SET CXX_MODULES FILES
      Shape.cpp
)
set_target_properties(foo PROPERTIES LINKER_LANGUAGE CXX)
# Create an executable
add_executable(cpp2test main.cpp2)
# Link to the library foo
target_link_libraries(cpp2test PUBLIC foo)

with Shape.cpp

export module Shape;

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

and main.cpp2

import Shape;

main : () = {
           std::cout << "Hello, world! " << add(1, 2) << "\n";
}

this compiles and runs. but i get errors as soon as i rename Shape.cpp to Shape.cpp2:

[cmake] CMake Error in CMakeLists.txt:
[cmake]   Target "foo" has source file
[cmake] 
[cmake]     /home/madam/Documents/cpp2test/Shape.cpp2
[cmake] 
[cmake]   in a "FILE_SET TYPE CXX_MODULES" but it is not scheduled for compilation.

i already added set_target_properties(foo PROPERTIES LINKER_LANGUAGE CXX), but it appears that that's not enough.

adam-ce commented 1 month ago

i just saw, that https://github.com/modern-cmake/cppfront/pull/145 appears to fix it, but is not merged and the corresponding bug is closed.

alexreinking commented 1 month ago

@adam-ce -- I meant to merge that, but I got distracted by the various CI issues and then my day job got busier. Do you have any bandwidth to help out with CI?

adam-ce commented 1 month ago

you're thinking the issue with macos?

i'm not a CI expert at all and i'm not really familiar with mac. in my own setup i ended up deleting the CI tests on Macos because they were failing too often (timeouts as well, but i was starting an android simulator).

besides that, no. i don't have available bandwidth, not even for my own pet project :/