[CMake] Clen-up pre-built software

Michael Hertling mhertling at online.de
Tue Nov 29 18:08:18 EST 2011


On 11/22/2011 11:42 AM, t m wrote:
> Hi Community,
> 
> In the large project we a having periodic taks which build a couple of
> projects and store the result of the build in one specific place.
> Once it's build the rest of the team can use this artifacts becouse
> those are exported by using of export(TARGETS ...)
> 
> Since the others refer only to the targets I would like to clean-up
> the common builds from optional artifacts in order to save some disk
> space.
> I guess the objects could be removed. The problem is that "make clean"
> will clean also executables and libraries, so it's not an option for
> me.
> 
> 1) Is there any way to get all  objects files as a list, so I can
> remove them in the loop ?
> 2) Is there any other way to cope with this clean-up ?
> 
> Looking for hints.
> 
> Best regards, T Majchrowski.

If you

- don't want to follow Hendrik's advice - although it's an absolutely
  reasonable suggestion - and still use EXPORT(TARGETS ...) instead
  of INSTALL(EXPORT ...), and
- get along with Makefiles,

you might use a RULE_LAUNCH property in conjunction with a shell script
or the like to collect the object files while your project is built:

# CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(OBJCLEAN C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
SET(ARTIFACTS "${CMAKE_BINARY_DIR}/artifacts.txt")
SET(COLLECT "${CMAKE_SOURCE_DIR}/collect.sh")
SET(REMOVE "${CMAKE_SOURCE_DIR}/remove.sh")
FILE(WRITE "${ARTIFACTS}")
SET_PROPERTY(GLOBAL PROPERTY RULE_LAUNCH_COMPILE
    "bash ${COLLECT} ${ARTIFACTS} <OBJECT>")
ADD_SUBDIRECTORY(subdir)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.c)
TARGET_LINK_LIBRARIES(main f g h)
ADD_CUSTOM_TARGET(objclean bash "${REMOVE}" "${ARTIFACTS}")

# subdir/CMakeLists.txt:
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/f.c "void f(void){}\n")
ADD_LIBRARY(f SHARED f.c)
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/g.c "void g(void){}\n")
ADD_LIBRARY(g SHARED g.c)
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/h.c "void h(void){}\n")
ADD_LIBRARY(h SHARED h.c)

# collect.sh:
F="$1"; A="$2"; shift 2
echo "$PWD/$A" >> "$F"
T=$(mktemp); sort -u "$F" > "$T"; mv "$T" "$F"
exec "$@"

# remove.sh:
for i in $(cat "$1"); do rm -f "$i"; done

The collect.sh script intercepts each compilation and logs the object
file with full path in artifacts.txt; the subdir just serves to show
that this works also with a directory hierarchy. Finally, the custom
target objclean invokes the remove.sh script which removes the files
listed in artifacts.txt.

Regards,

Michael


More information about the CMake mailing list