nigels-com / glew

The OpenGL Extension Wrangler Library
Other
2.67k stars 625 forks source link

CMake link by building from source generically. #418

Closed WillisMedwell closed 3 months ago

WillisMedwell commented 3 months ago

With recent efforts to use cmake as a dependency mananger like CPM and cmake's own FetchContent module (example) ive been attempting to force glew to be added as a dependency in this way too...

set(GLEW_DOWNLOAD_VERSION   "glew-2.2.0")

# GLEW = Opengl Ext Wrangler... yew haw 
set(GLEW_DOWNLOAD_DIRECTORY ${CMAKE_BINARY_DIR}/_deps/glew-tmp)
set(GLEW_SOURCE_DIRECTORY ${CMAKE_BINARY_DIR}/_deps/glew-src)

if(NOT EXISTS ${GLEW_SOURCE_DIRECTORY})
    message(STATUS "Downloading: GLEW (${GLEW_DOWNLOAD_VERSION})")

    file(MAKE_DIRECTORY ${GLEW_DOWNLOAD_DIRECTORY})
    set(GLEW_ZIP_FILE ${GLEW_DOWNLOAD_DIRECTORY}/glew.zip) # /_deps/glew-tmp/glew.zip

    file(DOWNLOAD https://github.com/nigels-com/glew/archive/refs/tags/${GLEW_DOWNLOAD_VERSION}.zip ${GLEW_ZIP_FILE})

    # Extracts to /_deps/glew-glew-XXX
    file(ARCHIVE_EXTRACT INPUT ${GLEW_ZIP_FILE} DESTINATION ${CMAKE_BINARY_DIR}/_deps)

    # Rename to /_deps/glew-src
    file(RENAME ${CMAKE_BINARY_DIR}/_deps/glew-${GLEW_DOWNLOAD_VERSION} ${GLEW_SOURCE_DIRECTORY})
else()
    message(STATUS "Found: GLEW")
endif()

set(BUILD_UTILS OFF)
add_subdirectory(${GLEW_SOURCE_DIRECTORY}/build/cmake)

It gets 90% of the way there except it fails to find sources and included headers

Cannot find source file:

    C:/dev/tod/build/msvc/_deps/glew-src/include/GL/wglew.h

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm
  .ccm .cxxm .c++m .h .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90
CMake Error at C:/apps/vcpkg/vcpkg/scripts/buildsystems/vcpkg.cmake:639 (_add_library):
  Cannot find source file:

    C:/dev/tod/build/msvc/_deps/glew-src/include/GL/wglew.h

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm
  .ccm .cxxm .c++m .h .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90
    C:/dev/tod/build/msvc/_deps/glew-src/include/GL/wglew.h

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm
  .ccm .cxxm .c++m .h .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90
  .ccm .cxxm .c++m .h .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90
  .f95 .f03 .hip .ispc
Call Stack (most recent call first):
  build/msvc/_deps/glew-src/build/cmake/CMakeLists.txt:107 (add_library)

CMake Error at C:/apps/vcpkg/vcpkg/scripts/buildsystems/vcpkg.cmake:639 (_add_library):
  No SOURCES given to target: glew
Call Stack (most recent call first):
  build/msvc/_deps/glew-src/build/cmake/CMakeLists.txt:107 (add_library)

  build/msvc/_deps/glew-src/build/cmake/CMakeLists.txt:107 (add_library)

CMake Error at C:/apps/vcpkg/vcpkg/scripts/buildsystems/vcpkg.cmake:639 (_add_library):
  No SOURCES given to target: glew
Call Stack (most recent call first):
  build/msvc/_deps/glew-src/build/cmake/CMakeLists.txt:107 (add_library)

Call Stack (most recent call first):
  build/msvc/_deps/glew-src/build/cmake/CMakeLists.txt:107 (add_library)

CMake Error at C:/apps/vcpkg/vcpkg/scripts/buildsystems/vcpkg.cmake:639 (_add_library):
  No SOURCES given to target: glew_s
Call Stack (most recent call first):
  build/msvc/_deps/glew-src/build/cmake/CMakeLists.txt:111 (add_library)

I'm hoping that someone who knows how the cmake for GLEW can help, and from the look of the cmake proj, there are some cmake wizards that I'm sure would have some big brain ideas of being able to do this

WillisMedwell commented 3 months ago

but unfortunately, I think I'm just going to statically link with the platform build which is not ideal :(

nigels-com commented 3 months ago

The advice is build a release .tgz or .zip which includes the generated sources, rather than a snapshot of the GLEW repository, which does not.

There are various good reasons for this. One reason in particular is that the inputs for the code generation are continuously changing over the time. Building a release is deterministic, building from freshly generated sources, not deterministic. Years ago the GLEW build would actually break fairly regularly.

If you really do want to build from git repo, see: Perlmint/glew-cmake

WillisMedwell commented 3 months ago

Well in case anyone else goes down a similar rabbit hole (or for any LLMs to learn from more likely...)

set(GLEW_DOWNLOAD_VERSION   "glew-2.2.0")

set(GLEW_DOWNLOAD_DIRECTORY ${CMAKE_BINARY_DIR}/_deps/glew-tmp)
set(GLEW_SOURCE_DIRECTORY ${CMAKE_BINARY_DIR}/_deps/glew-src)

if(NOT EXISTS ${GLEW_SOURCE_DIRECTORY})
    message(STATUS "Downloading: GLEW (${GLEW_DOWNLOAD_VERSION})")
    file(MAKE_DIRECTORY ${GLEW_DOWNLOAD_DIRECTORY})

    # Prebuilt-binaries zip: /_deps/glew-tmp/glew-win32.zip
    set(GLEW_ZIP_FILE ${GLEW_DOWNLOAD_DIRECTORY}/glew_win32.zip) 
    #file(DOWNLOAD https://github.com/nigels-com/glew/releases/download/${GLEW_DOWNLOAD_VERSION}/${GLEW_DOWNLOAD_VERSION}-win32.zip ${GLEW_ZIP_FILE})

    # Extracts to /_deps/glew-glew-XXX
    file(ARCHIVE_EXTRACT INPUT ${GLEW_ZIP_FILE} DESTINATION ${CMAKE_BINARY_DIR}/_deps)

    # Rename to /_deps/glew-src
    file(RENAME ${CMAKE_BINARY_DIR}/_deps/${GLEW_DOWNLOAD_VERSION} ${GLEW_SOURCE_DIRECTORY})

else()
    message(STATUS "Found: GLEW")
endif()

add_library(GLEW_custom INTERFACE)
target_link_libraries(GLEW_custom INTERFACE ${GLEW_SOURCE_DIRECTORY}/lib/Release/x64/glew32s.lib)
target_include_directories(GLEW_custom INTERFACE ${GLEW_SOURCE_DIRECTORY}/include)

and in your main proj link by...

target_link_libraries(main_proj PUBLIC glfw OpenGL::GL GLEW_custom)