How to replace FetchContent_Declare to copy the already cloned local repo

688 Views Asked by At

We are using CMakeLists.txt for compiling our huge code base. We are cloning the repo at compilation stage, using FetchContent_Declare as below.

FetchContent_Declare(miniz
  GIT_REPOSITORY https://github.com/richgel999/miniz.git
  GIT_TAG        2.0.8
)
FetchContent_Populate(miniz)

I would like to use the code which is already cloned in my local machine, something like using file(COPY mydir DESTINATION mydest) or may be there is other solution, which I am not aware.

Now if I use above file command I am getting below error:

Cannot find source file:
  //miniz.c

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
  .hpp .hxx .in .txx

miniz.c is already part of my git cloned repo.

Is there a way to declare target, which is same as FetchContent_Declare? Is there any other alternative to copy it from local and set the parameter which is set by FetchContent_Declare and FetchContent_Populate?

In short I would like to replace the git fetch to local fetch without changing much line of code in our huge repo. Since the target 'miniz' is being used in sub directories. And I want to just change the parent CMakeLists.txt.

You help will be more appreciated.

1

There are 1 best solutions below

0
starball On

From what you've shown, you didn't add_subdirectory the declared thing to fetch. You should either use FetchContent_MakeAvailable, or FetchContent_Populate + add_subdirectory as shown in the examples in the docs.

If you already have a clone of the desired remote repo and tag on your local machine, then on your local machine, when writing your configure command, set the FETCHCONTENT_SOURCE_DIR_<uppercaseName> variable. In your case, it would be with a commandline argument like -DFETCHCONTENT_SOURCE_DIR_MINIZ:PATH=</put/the/absolute/path/to/your/local/clone/here>. If you later get annoyed by having to write out that flag in your configure command (although I can't imagine many circumstances where you'd need to, since -D variables get cached for reconfigures), use create a CMakeUserPresets.json.

You would do well to read the docs more carefully because there are other little important things to know (I'd mention specific here, but I don't know which specifics are relevant to you).