[CMake] Parsing command line arguments from the make

Fedja Jeleskovic mrawd2 at gmail.com
Tue Apr 5 15:24:47 EDT 2016


I ended up using this exact approach with addition of one top level
Makefile which is used to run cmake in build folder. Top level Makefile
takes those arguments and passes them down to the cmake execution, which
than makes a decision on which way to go depending on the argument passed
in.

Here is how that part looks like:
BUILD_DIR   :=  build

ifneq ($(ENV),)
DEPLOY=$(ENV)
endif

ifeq "$(DEPLOY)" "V1"
NEW_FLAG=-DVERSION_1=ON
else
ifeq "$(DEPLOY)" "V2"
NEW_FLAG = -DVERSION_2=ON
else
ifeq "$(DEPLOY)" "V3"
NEW_FLAG = -DVERSION_3=ON
endif
endif
endif

all: ${BUILD_DIR}/Makefile
$(MAKE) -C ${BUILD_DIR}

cmake:
touch CMakeLists.txt
$(MAKE) ${BUILD_DIR}/Makefile

${BUILD_DIR}/Makefile:
@[ -d ${BUILD_DIR} ] || mkdir -p ${BUILD_DIR}
@[ -f ${BUILD_DIR}/Makefile ] || (cd ${BUILD_DIR} && cmake $(NEW_FLAG) ..)
touch $@


I have also created three options (needed three in my case) that create
#defines for each of them and depending on which is initialized at the
start, ends up being enabled in the source code.

The issue that I had at the end was to clean cached values of those since
regular clean wasn't clearing #define that was previously set. At the end I
am running "make clean"  in build folder, following with delete of
build/CMakeCache.txt and build/Makefile. After that new option passed in to
the top level Makefile does what I need (initialize proper #define that is).

Here is how the clean section looks like in the top level Makefile:
.PHONY : clean
clean:
@- [ -f ${BUILD_DIR}/Makefile ] && $(MAKE) --silent -C ${BUILD_DIR} clean
|| true
@- [ -f ${BUILD_DIR}/CMakeCache.txt ] && rm ${BUILD_DIR}/CMakeCache.txt ||
true
@- [ -f ${BUILD_DIR}/Makefile ] && rm -rf ${BUILD_DIR}/Makefile || true


If there is a better way to clear the cache so I don't have to delete those
two files, I would like someone to point it.

Thanks for help and hope this will be useful for someone else in the future!

Any other comments on this are welcome!

On Tue, Apr 5, 2016 at 4:36 AM, Matějů Miroslav, Ing. <
Mateju.Miroslav at azd.cz> wrote:

> Hi Fedja,
>
>
>
> As far as I know, the Makefiles generated  from CMake cannot contain
> decisions. CMake supports several output types aside from Makefiles and
> some of them probably don’t support decisions. However, you could supply
> these arguments within CMake call using -D option. For example
>
>     cmake -DENV=VERSION_2 <source_or_binary_directory>
>
> creates a CMake variable just like
>
>     set(ENV "VERSION_2" CACHE)
>
> in the CMake source file.
>
>
>
> As you’ve mentioned already, you can access environment variables using
> $ENV{variable} syntax in CMake.
>
>
>
> Hope this helps.
>
>
>
> Miroslav
>
>
>
> *From:* CMake [mailto:cmake-bounces at cmake.org] *On Behalf Of *Fedja
> Jeleskovic
> *Sent:* Friday, April 01, 2016 8:08 PM
> *To:* cmake at cmake.org
> *Subject:* [CMake] Parsing command line arguments from the make
>
>
>
> Since I am converting existing makefile project to use cmake instead I
> need to accept values that come from command line which looks like this:
> VARIABLE_NAME="/home/user/project" make ENV=VERSION_2
>
>
>
> First one is used like this:
>
> include $(VARIABLE_NAME)/Makefile.include
>
>
>
> Second one has this code that triggers different paths later:
> ifneq ($(ENV),)
>
> DEPLOYMENT_VERSION=$(ENV)
>
> endif
>
>
>
> How do I do this in cmake?
>
>
>
> Thanks!
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160405/fba69315/attachment.html>


More information about the CMake mailing list