Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CXX-Interop: Fix missing header deps edge #9

Merged
merged 1 commit into from
Apr 29, 2024

Conversation

etcwilde
Copy link
Member

The custom target dependency was an order-only dependency edge. As a result, changing the swift file wouldn't result in the header getting regenerated at the right time because nothing actually depended on it being up-to-date. This patch should fix that and ties it into the fibonacci target a little more cleanly. The generated header is marked as being one of the target sources, so the target itself depends on the header getting built. This ensures that the C++ file gets rebuilt if the header changes. The header still depends on the Swift sources in the target. We're now extracting that list directly from the target itself instead of needing to pass it in explicitly. The module is also extracted from the target itself. We're also automatically adding the location of the header include directory so that dependees can find it as part of the target interface.

Fixes #8

@etcwilde etcwilde requested a review from compnerd April 20, 2024 22:20
@etcwilde
Copy link
Member Author

@afabri, I think this should fix the issue. I've tied the header generation into the target itself a little more tightly and am now seeing the header recognized as one of the dependencies in the generated dependency file from clang.

I'm also seeing that modifying the Swift file marks the header as dirty, which then cascades to the C++ file and onward resulting in a full rebuild as expected.

ninja explain: restat of output lib/fibonacci/include/fibonacci/fibonacci-swift.h older than most recent input /Users/ewilde/Work/swift-cmake-examples/3_bidirectional_cxx_interop/lib/fibonacci/fibonacci.swift (1713650908121029567 vs 1713650920687477075)
ninja explain: /Users/ewilde/Work/swift-cmake-examples/3_bidirectional_cxx_interop/build/lib/fibonacci/include/fibonacci/fibonacci-swift.h is dirty
ninja explain: lib/fibonacci/CMakeFiles/fibonacci.dir/fibonacci.cpp.o is dirty
ninja explain: lib/fibonacci/libfibonacci.a is dirty
...

@etcwilde etcwilde force-pushed the ewilde/fix-cxx-interop-deps-graph branch from 25f01fe to bef44f8 Compare April 26, 2024 01:41
@etcwilde etcwilde requested a review from afabri April 26, 2024 01:55
Copy link

@afabri afabri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not being an expert in CMake scripts I can just approve that what you did works for me.

Copy link

@lrineau lrineau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I have a few minor remarks.

cmake_parse_arguments(ARG "" "" "SEARCH_PATHS;MODULE_NAME" ${ARGN})

if(NOT ARG_MODULE_NAME)
set(ARG_MODULE_NAME $<TARGET_PROPERTY:${target},Swift_MODULE_NAME>)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there should be a validation of ${ARG_MODULE_NAME}.

I do not know if all strings are a valid swift module names, but I imagine it is not the case.

At the very least, the code should message(FATAL_ERROR ...) if ARG_MODULE_NAME is empty (when both the argument to the function and the target property are empty or not defined).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validation of the module name at the cmake level would be more brittle than allowing the compiler to determine what it considers to be a valid module name than trying to replicate the logic here.

As for the empty case, that's a good point. If unset, CMake will default the module name to the name of the target, so I'll do the same with the generated header to match.

Comment on lines 41 to 42
COMMENT
"Generating '${header}'"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why the COMMENT was removed.

The custom target dependency was an order-only dependency edge. As a
result, changing the swift file wouldn't result in the header getting
regenerated at the right time because nothing actually depended on it
being up-to-date. This patch should fix that and ties it into the
`fibonacci` target a little more cleanly. The generated header is marked
as being one of the target sources, so the target itself depends on the
header getting built. This ensures that the C++ file gets rebuilt if the
header changes. The header still depends on the Swift sources in the
target. We're now extracting that list directly from the target itself
instead of needing to pass it in explicitly. The module is also
extracted from the target itself. We're also automatically adding the
location of the header include directory so that dependees can find it
as part of the target interface.

Fixes apple#8
@etcwilde etcwilde force-pushed the ewilde/fix-cxx-interop-deps-graph branch from bef44f8 to 51d1ba4 Compare April 27, 2024 05:54
Copy link

@lrineau lrineau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks you @etcwilde. Looks good to me.

@etcwilde etcwilde merged commit e673cd1 into apple:main Apr 29, 2024
@etcwilde etcwilde deleted the ewilde/fix-cxx-interop-deps-graph branch April 29, 2024 22:52
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.

Accessing a swift struct from C++ in ping pong
4 participants