0

I have a cmake project leveraging another cmake library project, the dir looks like this:

/CMakeList.txt /main.c /.gitmodules /patch/patch.c /vendor/CMakeList.txt /vendor/vendor.c /vendor/... 

In /CMakeList.txt, I used add_subdirectory(vendor) to include/link vendor library to my own project.

In /vendor/CMakeList.txt, source file is added by FILE(GLOB SRC_VEN vendor.c ...)

I want to inject /patch/patch.c into vendor library without touching /vendor directory, which is a git submodule

Is it possible to use cmake to achieve this in my root CMakeList.txt?

Thanks!

4
  • how does /vendor/CMakeList.txt really look like? Doesn't it use a wildcard in FILE(GLOB ...)? What do you mean "without touching"? Could any files be changed automatically? Commented Jul 29, 2015 at 12:25
  • 1
    Something like target_sources(vendor_target PRIVATE "patch/patch.c") after your add_subdirectory(vendor) call? But you need to know the target's name you want to inject into (vendor_target is just a placeholder). See here for more details. Commented Jul 29, 2015 at 12:34
  • Hi @m.s. thanks for the reply, yes /vendor/CMakeLists.txt uses wildcard, i mentioned 'without touch' vendor because I can modify /vendor/CMakeLists.txt a little bit, and ignore=dirty in my .gitmodules so that the change will not be committed Commented Jul 29, 2015 at 13:39
  • Thank you, @Florian ! just tested, target_sources works like a charm Commented Jul 29, 2015 at 13:52

1 Answer 1

2

Turning my comment into an answer

If you want to inject sources into an existing target you can use target_sources() (introduced with CMake version 3.1).

In your case this would look something like:

/CMakeLists.txt

... add_subdirectory(vendor) target_sources(vendor_target PRIVATE "patch/patch.c") 

I put vendor_target as a placeholder. Please replace it with the real name for the target you want to inject the sources into.

For more details see e.g. here.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.