Help Accommodating Ninja when including CMAKE_CURRENT_BINARY_DIR

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Help Accommodating Ninja when including CMAKE_CURRENT_BINARY_DIR

Stephen McDowell
Hello,

I have looked around and seen a few issues discussing this, but have not seen any solutions.


In the sense that absolute paths are desired.

1. Previously, we were generating a file called `${CMAKE_CURRENT_BINARY_DIR}/nanogui_resources.h` and performing `include_directories(${CMAKE_CURRENT_BINARY_DIR})`.

    This **works correctly**.

2. The setup was changed to generate the file `${CMAKE_CURRENT_BINARY_DIR}/nanogui/resources.h`.  That is, the introduction of a new folder `nanogui`.  With the same `include_directories(${CMAKE_CURRENT_BINARY_DIR})`, there is now a failure being unable to find `<nanogui/resources.h>`.

    Strangely, building with `ninja || ninja` (just building again after first failure) will succeed.


and the underlying cause seems to be the that Ninja wants relative paths.

Why is creating this sub-directory causing this issue?  Is there a way to keep generating ${CMAKE_CURRENT_BINARY_DIR}/nanogui and be able to include it with Ninja?  I tried forcing a add_definitions(-I${CMAKE_CURRENT_BINARY_DIR}) just for shiggles, but that resulted in the same scenario.  So it seems like I need a way to force a verbatim include path “after” the Ninja generator makes things relative?

Reverting to the old version is not ideal, since it creates installation problems (the reason the subdirectory nanogui was created).

Thank you for any advice!

-Stephen

P.S. This is with CMake 3.11.1 and ninja 1.8.2 if it matters.


--

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:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Help Accommodating Ninja when including CMAKE_CURRENT_BINARY_DIR

Stephen McDowell
Upon further inspection, it seems more related to PRE_BUILD.  The step to generating the files is through a add_custom_command call, which from the docs apparently may end up being PRE_LINK.

I tried generating into the ${CMAKE_CURRENT_SOURCE}/include/nanogui, which will work for Makefiles but not for Ninja.

So it seems that maybe this is not a pathing issue at all, but instead an order of operations issue.  If the Ninja generator ends up having this as PRE_LINK, the files haven’t been generated yet.

That doesn’t quite explain why it worked previously, but perhaps the original setup was inherently flawed by relying on PRE_BUILD which is not a guaranteed order.

Does anybody have thoughts on how to change things?  The files cannot be compiled on their own, but maybe I should instead create a fake target and make it a dependency of the real (object) library that needs them.  I keep messing that up, but this seems the most promising.

-Stephen

--

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:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Help Accommodating Ninja when including CMAKE_CURRENT_BINARY_DIR

Robert Maynard
I recommend going with the custom target / custom command route.

On Fri, Apr 27, 2018 at 5:19 AM, Stephen McDowell <[hidden email]> wrote:
Upon further inspection, it seems more related to PRE_BUILD.  The step to generating the files is through a add_custom_command call, which from the docs apparently may end up being PRE_LINK.

I tried generating into the ${CMAKE_CURRENT_SOURCE}/include/nanogui, which will work for Makefiles but not for Ninja.

So it seems that maybe this is not a pathing issue at all, but instead an order of operations issue.  If the Ninja generator ends up having this as PRE_LINK, the files haven’t been generated yet.

That doesn’t quite explain why it worked previously, but perhaps the original setup was inherently flawed by relying on PRE_BUILD which is not a guaranteed order.

Does anybody have thoughts on how to change things?  The files cannot be compiled on their own, but maybe I should instead create a fake target and make it a dependency of the real (object) library that needs them.  I keep messing that up, but this seems the most promising.

-Stephen

--

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:
https://cmake.org/mailman/listinfo/cmake


--

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:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Help Accommodating Ninja when including CMAKE_CURRENT_BINARY_DIR

Stephen McDowell

​After much deliberation, I was never successful in getting a synthetic target to work as expected.  The dilemma is that `add_custom_command` cannot specify both `TARGET` and `OUTPUT`, the work flow was something like

add_custom_target(resource-dependency)
add_custom_command(TARGET resource-dependency ... PRE_BUILD)
add_custom_command(OUTPUT <files that are generated> DEPENDS resource-dependency)
...
add_library(actual-obj-library OBJECt <files that are generated> ... others ...)
add_dependencies(actual-obj-library resource-dependency)

I'm sure that's a flawed approach, but in the end I realized I can actually generate these files at *configure* time <3  The add_custom_command was just a call to ${CMAKE_COMMAND} ... resources/bin2c.cmake.  So in the end, it's actually a lot cleaner for us to just include(resources/bin2c.cmake).

I doubt this solution will be widely applicable to many projects, but if you ever find yourself in a situation where you are doing add_custom_command, where the custom command is calling ${CMAKE_COMMAND}, don't rely on PRE_BUILD because it's not a guarantee.  If you can, just execute the script at configure time via an include(...) call.

--

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:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Help Accommodating Ninja when including CMAKE_CURRENT_BINARY_DIR

Robert Maynard
Here is how I would do this (
https://github.com/robertmaynard/Sandbox/tree/master/CMakeCustomCommandAfterTarget).
In my example the executable is the target I want to have a custom
PRE_BUILD step for.
On Sat, Apr 28, 2018 at 5:02 AM Stephen McDowell <[hidden email]> wrote:


> After much deliberation, I was never successful in getting a synthetic
target to work as expected.  The dilemma is that `add_custom_command`
cannot specify both `TARGET` and `OUTPUT`, the work flow was something like

> add_custom_target(resource-dependency)
> add_custom_command(TARGET resource-dependency ... PRE_BUILD)
> add_custom_command(OUTPUT <files that are generated> DEPENDS
resource-dependency)
> ...
> add_library(actual-obj-library OBJECt <files that are generated> ...
others ...)
> add_dependencies(actual-obj-library resource-dependency)

> I'm sure that's a flawed approach, but in the end I realized I can
actually generate these files at *configure* time <3  The
add_custom_command was just a call to ${CMAKE_COMMAND} ...
resources/bin2c.cmake.  So in the end, it's actually a lot cleaner for us
to just include(resources/bin2c.cmake).

> I doubt this solution will be widely applicable to many projects, but if
you ever find yourself in a situation where you are doing
add_custom_command, where the custom command is calling ${CMAKE_COMMAND},
don't rely on PRE_BUILD because it's not a guarantee.  If you can, just
execute the script at configure time via an include(...) call.
--

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:
https://cmake.org/mailman/listinfo/cmake