cmake - preventing `make clean` from cleaning Exte

2019-06-22 02:05发布

问题:

I was wondering if there's some way to prevent make clean in cmake from re-building external dependencies. I'm using ExternalProject to build third party c++ libraries, and they do not have to be rebuilt even if I do make clean.

On the other hand, I might want to create a new rule, say, make really-clean, which even clears the dependencies. is there a good way to do this?

Thanks.

回答1:

I assume you use ADD_CUSTOM_COMMAND, or ADD_LIBRARY or other ADD_* to create the dependency files.

If your ExternalProject has it's own directory and you can put CMakeLists.txt in that directory, you can easy put following in that CMakeLists.txt:

SET_DIRECTORY_PROPERTIES(PROPERTIES CLEAN_NO_CUSTOM 1)

So those dependency files won't get cleaned.

If not, you may need to avoid to put the external dependencies as the output files. For example, if you use

ADD_CUSTOM_COMMAND(OUTPUT libdep
   COMMAND dep_gen_cmd
   ....
)

ADD_CUSTOM_COMMAND(OUTPUT prj
   ....
   DEPENDS libdep
)

then you need to change it to:

ADD_CUSTOM_TARGET(libdep_gen
      COMMAND dep_gen_cmd
      ...
)

ADD_CUSTOM_COMMAND(OUTPUT prj
     COMMAND test -e libdep || make libdep_gen
     ...
)