[CMake] Get native build command with CMake 3+

Petr Kmoch petr.kmoch at gmail.com
Fri Dec 5 10:41:20 EST 2014


OK, sorry for misunderstanding. But then I don't have a target like this.
Basically, I am trying to create a custom target that would do this:

# When using Makefile generator
add_custom_target(
  examples
  COMMAND make -C example_dir all
  VERBATIM
)

# When using vs2008 generator
add_custom_target(
  examples
  COMMAND devenv Examples.sln /Build /Project:ALL_BUILD
/ProjectConfig:$<CONFIG>
  WORKING_DIRECTORY example_dir
  VERBATIM
)

# When using vs2010 generator, use msbuild
# etc...

without having to hardcode the logic (CMake must have this info inside
somewhere). So I am effectively trying to *create* the target 'examples'.

My current solution is to make the custom target empty and have it depend
on all the targets in example_dir. But this has two consequences I don't
like:
1. I have to collect the target names in example_dir/CMakeLists.txt, as it
has to communicate them to its parent
2. Being dependecies, the targets from example_dir show up in the toplevel
solution file/Makefile. I was trying to prevent that.

I looked at the commit message which changed build_command() from returning
the native command line to returning cmake --build, at it mentions the fact
that sometimes, the native tool has to be selected based on the contents of
the solution (devenv instead of msbuild when ifort is involved). So I guess
the only way to get what I want would be with a generator expression (one
which does not currently exist).

I guess I'll stick to my workaround with dependencies. But thanks for your
help.

Petr

On Fri, Dec 5, 2014 at 4:19 PM, David Cole <DLRdave at aol.com> wrote:

> No, I meant exactly what I said.
>
> --target takes a CMake target name.
>
>
> HTH,
> D
>
>
>
> On Fri, Dec 5, 2014 at 9:23 AM, Petr Kmoch <petr.kmoch at gmail.com> wrote:
> > I assume you actually meant 'cmake --build . --target example_dir', the
> name
> > of the directory. 'examples' is the name of the custom target; if that
> > target's command was to build itself, I believe it would become a fork
> bomb.
> >
> > Anyway, I tried it with the directory name, it doesn't work. I'm
> generating
> > with VS2008, and devenv was complaining about "Invalid project." Which
> kind
> > of makes sense, there is no 'example_dir' project in the solution.
> >
> > On Fri, Dec 5, 2014 at 2:59 PM, David Cole <DLRdave at aol.com> wrote:
> >>
> >> Try:
> >>
> >> cmake --build . --target examples
> >>
> >> (where "." represents the current working directory, and assumes
> >> you're in the top level build tree...)
> >>
> >>
> >> On Fri, Dec 5, 2014 at 5:46 AM, Petr Kmoch <petr.kmoch at gmail.com>
> wrote:
> >> > Hi all.
> >> >
> >> > I'm converting a small Makefile-based project to CMake. The project is
> >> > not
> >> > mine, so I am trying to match its existing buildsystem as closely as
> >> > possible.
> >> >
> >> > One of the rules in the original Makefile is (simplified) as follows:
> >> >
> >> > examples: all
> >> >   make -C example_dir all
> >> >
> >> > This gives a target 'examples' in the top-level Makefile, which is not
> >> > included in 'all', but can be used to build all examples. I would like
> >> > to
> >> > achieve something similar with CMake.
> >> >
> >> > I added example_dir/CMakeLists.txt, which includes a project() command
> >> > and
> >> > defines all the targets for example executables. In the toplevel
> >> > CMakeList,
> >> > I did this:
> >> >
> >> > # Add the subdirectory but don't include its targets by default
> >> > add_subdirectory(example_dir EXCLUDE_FROM_ALL)
> >> >
> >> > # Add a custom target to build all examples
> >> > add_custom_target(examples ???)
> >> >
> >> > I'm stuck at the custom target. What I need for its command is a
> >> > generator-agnostic equivalent for 'make -C example_dir all' (or 'make
> -f
> >> > example_dir/Makefile all').
> >> >
> >> > I tried 'cmake --build ${CMAKE_CURRENT_BINARY_DIR}/example_dir ...',
> but
> >> > this complains that example_dir does not contain a CMakeCache.txt
> (which
> >> > is
> >> > correct of course, it's not the toplevel directory). Then I remembered
> >> > that
> >> > build_command() can be used to get a command line to build a project;
> >> > unfortunately, in 3.0 and later, this just returns 'cmake --build ...'
> >> > and
> >> > so is unusable in my situation.
> >> >
> >> > Is there a way to query CMake for the native command line (or
> something
> >> > else) I could use to build the CMake-generated buildsystem in the
> >> > subdirectory? I thought a bit about CMAKE_MAKE_PROGRAM, but that
> doesn't
> >> > tell me anything about what arguments I should pass, and it's not even
> >> > set
> >> > on Visual Studio generators.
> >> >
> >> > As a workaround, I thought of not doing add_subdirectory() at all and
> >> > instead adding a custom target with 'ctest --build-and-test ...', but
> I
> >> > would prefer to configure & generate the two directories together. I
> >> > plan to
> >> > eventually add install() rules for the examples as well, and the
> >> > examples
> >> > also use one of the targets from the toplevel cmakelist in
> >> > add_custom_command(OUTPUT ...), so I would really like to keep them as
> >> > one
> >> > CMake system. Is there a way to do so?
> >> >
> >> > Petr
> >> >
> >> > --
> >> >
> >> > Powered by www.kitware.com
> >> >
> >> > Please keep messages on-topic and check the CMake FAQ at:
> >> > http://www.cmake.org/Wiki/CMake_FAQ
> >> >
> >> > Kitware offers various services to support the CMake community. For
> more
> >> > information on each offering, please visit:
> >> >
> >> > CMake Support: http://cmake.org/cmake/help/support.html
> >> > CMake Consulting: http://cmake.org/cmake/help/consulting.html
> >> > CMake Training Courses: http://cmake.org/cmake/help/training.html
> >> >
> >> > Visit other Kitware open-source projects at
> >> > http://www.kitware.com/opensource/opensource.html
> >> >
> >> > Follow this link to subscribe/unsubscribe:
> >> > http://public.kitware.com/mailman/listinfo/cmake
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20141205/246aad3d/attachment-0001.html>


More information about the CMake mailing list