RenderKit / ospray

An Open, Scalable, Portable, Ray Tracing Based Rendering Engine for High-Fidelity Visualization
http://ospray.org
Apache License 2.0
1k stars 182 forks source link

CMake usage example #306

Closed nyue closed 5 years ago

nyue commented 5 years ago

I have use CMake in other projects but new to the CMake macros and library references in OSPRay distribution.

Does one use the macros to build application so that library linking is taken care of ?

The osprayConfig.cmake does not set many CMake variable with CACHE and string description has using ccmake does not show one what variables should be use, I am mostly discovering them by reading the *.cmake file as part of the distribution.

Does anyone have an example of building an application referencing an installed OSPRay distribution ?

Cheers

Twinklebear commented 5 years ago

Hey @nyue , I've got a side project which links with an installed OSPRay, have a look at rtobj. To find OSPRay you'll pass -Dospray_DIR=<path to osprayConfig.cmake> (should be somewhere like <ospray install>/lib/cmake/ospray-M.N.P. Then to include and link against OSPRay you can use the OSPRAY_INCLUDE_DIRS and OSPRAY_LIBRARIES cmake variables (see the CMakeLists.txt). This might change a little bit when we move to exporting CMake targets, but the old-style variables might still be exported for convenience.

For now, if you're using CMake 3+ in your cmake you'd likely just do:

find_package(ospray REQUIRED)

add_executable(yourapp ...)

target_include_directories(yourapp PUBLIC
        $<BUILD_INTERFACE:${OSPRAY_INCLUDE_DIR}>)

target_link_libraries(yourapp PUBLIC ${OSPRAY_LIBRARIES})

Or on CMake 2:

find_package(ospray REQUIRED)

include_directories(${OSPRAY_INCLUDE_DIR})

add_executable(yourapp ...)

target_link_libraries(yourapp ${OSPRAY_LIBRARIES})

In both cases you pass -Dospray_DIR=<path to osprayConfig.cmake> when you run cmake to find OSPRay.

jeffamstutz commented 5 years ago

Adding to @Twinklebear's response:

I have an even simpler example of ospTutorial built as a standalone project that uses an OSPRay install: located here

We also have a small blurb on how to use OSPRay from an install on our downloads page: located here

The biggest change in v1.8.0+ releases is that everything OSPRay needs is propagated through the ospray::ospray target. In other words, there's no need to do anything except:

target_link_libraries(your_app ospray::ospray) # add PUBLIC/PRIVATE/INTERFACE accordingly

...in order to propagate all link libraries, include directories, and preprocessor definitions. Since v1.8.0, OSRPay requires CMake v3.1+ for both compiling and using OSPRay (CMake 2.x is no longer supported). All usage of ${OSPRAY_INCLUDE_DIR} and ${OSPRAY_LIBRARIES} (and other variables) are deprectaed in favor of linking targets directly (i.e. ospray::ospray).

Our internal macros (namely ospray_create_libray() and ospray_create_application()) now only take care of compiling ISPC files and doing install() commands correctly. I do not recommend using them, as we are slowly phasing out all uses of special CMake macros that we can (at least macros that create new targets).

jeffamstutz commented 5 years ago

Feel free to ask any questions here about CMake stuff, and please reopen the issue if you encounter any bugs!