From afdb699b7fe0cc88a61498f11747e8fd8cb0f51f Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Mon, 6 Jul 2020 20:32:04 -0400 Subject: [PATCH] download and build freeglut if not available --- CMakeLists.txt | 10 --------- graphics/CMakeLists.txt | 49 ++++++++++++++++++++++++++++++++--------- graphics/spirograph.cpp | 11 +++------ 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a948749a..d7fa88559 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,16 +25,6 @@ if(USE_OPENMP) message(STATUS "No OpenMP found, no multithreading.") endif() endif() - -find_package(OpenGL) -if(NOT OpenGL_FOUND) - message(WARNING "OpenGL not found, not compiling graphics programs.") -else(NOT OpenGL_FOUND) - find_package(GLUT) - if(NOT GLUT_FOUND) - message(WARNING "GLUT not found, not compiling graphics programs.") - endif(NOT GLUT_FOUND) -endif(NOT OpenGL_FOUND) add_subdirectory(math) add_subdirectory(others) diff --git a/graphics/CMakeLists.txt b/graphics/CMakeLists.txt index 2e03a71ed..e4744e47a 100644 --- a/graphics/CMakeLists.txt +++ b/graphics/CMakeLists.txt @@ -1,3 +1,25 @@ +find_package(OpenGL REQUIRED) +find_package(GLUT) +if(NOT GLUT_FOUND) + message("FreeGLUT library will be downloaded and built.") + include(ExternalProject) + # Include GLFW + ExternalProject_Add ( + FREEGLUT + URL https://sourceforge.net/projects/freeglut/files/freeglut/3.2.1/freeglut-3.2.1.tar.gz + URL_MD5 cd5c670c1086358598a6d4a9d166949d + CMAKE_ARGS -DCMAKE_BUILD_TYPE=RelWithDebInfo + -DFREEGLUT_BUILD_SHARED_LIBS=ON + -DFREEGLUT_BUILD_STATIC_LIBS=ON + -DFREEGLUT_BUILD_DEMOS=OFF + PREFIX ${CMAKE_CURRENT_BINARY_DIR}/freeglut + BUILD_IN_SOURCE ON + INSTALL_COMMAND "" + ) + ExternalProject_Get_Property(FREEGLUT SOURCE_DIR) + set(FREEGLUT_DIR ${SOURCE_DIR}) +endif(NOT GLUT_FOUND) + # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. @@ -9,18 +31,25 @@ foreach( testsourcefile ${APP_SOURCES} ) string( REPLACE ".cpp" "" testname ${testsourcefile} ) add_executable( ${testname} ${testsourcefile} ) - set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) + # set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) - target_link_libraries(${testname} OpenMP::OpenMP_CXX) + target_link_libraries(${testname} PRIVATE OpenMP::OpenMP_CXX) endif() - if (GLUT_FOUND) - if(APPLE) - target_compile_options(${testname} PUBLIC -Wno-deprecated) - endif(APPLE) - target_compile_definitions(${testname} PUBLIC GLUT_FOUND) - target_include_directories(${testname} PUBLIC ${GLUT_INCLUDE_DIRS}) - target_link_libraries(${testname} OpenGL::GL ${GLUT_LIBRARIES}) - endif(GLUT_FOUND) + + if(NOT GLUT_FOUND) + add_dependencies(${testname} FREEGLUT) + target_include_directories(${testname} PRIVATE ${FREEGLUT_DIR}/include) + target_link_directories(${testname} PRIVATE ${FREEGLUT_DIR}/lib) + target_link_libraries(${testname} PRIVATE freeglut OpenGL::GL) + else() + target_include_directories(${testname} PRIVATE ${GLUT_INCLUDE_DIRS}) + target_link_libraries(${testname} PRIVATE OpenGL::GL ${GLUT_LIBRARIES}) + endif() + + if(APPLE) + target_compile_options(${testname} PRIVATE -Wno-deprecated) + endif(APPLE) + install(TARGETS ${testname} DESTINATION "bin/graphics") endforeach( testsourcefile ${APP_SOURCES} ) diff --git a/graphics/spirograph.cpp b/graphics/spirograph.cpp index 7e8ce21ac..8bfa9f6c0 100644 --- a/graphics/spirograph.cpp +++ b/graphics/spirograph.cpp @@ -25,13 +25,11 @@ #ifdef _OPENMP #include #endif -#ifdef GLUT_FOUND // this is set by CMAKE automatically, if available #ifdef __APPLE__ #include // include path on Macs is different #else #include #endif // __APPLE__ -#endif // GLUT_FOUND /** * @namespace spirograph Functions related to spirograph.cpp @@ -114,7 +112,6 @@ void test() { fp.close(); } -#ifdef GLUT_FOUND // this is set by CMAKE automatically, if available /** A wrapper that is not available in all GLUT implementations. */ static inline void glutBitmapString(void *font, char *message) { @@ -214,12 +211,13 @@ void timer_cb(int t) { glutTimerFunc(25, timer_cb, 0); glutPostRedisplay(); } -#endif + } // namespace spirograph /** Main function */ int main(int argc, char **argv) { -#ifdef GLUT_FOUND + spirograph::test(); + glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutCreateWindow("Spirograph"); @@ -228,9 +226,6 @@ int main(int argc, char **argv) { glutTimerFunc(25, spirograph::timer_cb, 0); glutDisplayFunc(spirograph::test2); glutMainLoop(); -#else - spirograph::test(); -#endif return 0; }