Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace git submodules with native cmake features #254

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
out
cmake-build*
build
*.DotSettings.user
*.DotSettings.user
compile_commands.json
.cache
10 changes: 0 additions & 10 deletions .gitmodules

This file was deleted.

52 changes: 32 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.14...3.25)
project(GLFWPP CXX)

function(add_subdirectory_checked dir)
if (NOT EXISTS ${dir}/CMakeLists.txt)
message(FATAL_ERROR
"Error: Subdirectory " ${dir} " not found\n"
"You may have cloned the repository without the --recursive flag\n"
"Use `git submodule update --init --recursive` to get the required dependencies"
)
endif ()
add_subdirectory(${dir})
endfunction()

#Options
option(GLFWPP_BUILD_EXAMPLES "Should examples be built" ON)

Expand All @@ -26,19 +15,42 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
set(CMAKE_CXX_LINKER_WRAPPER_FLAG "")
target_link_options(GLFWPP INTERFACE "LINKER:-sUSE_GLFW=3")
else()
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory_checked(${CMAKE_CURRENT_SOURCE_DIR}/external/glfw)
target_link_libraries(GLFWPP INTERFACE glfw)
# try to find with cmake
find_package(glfw3 CONFIG)
if(glfw3_FOUND)
target_link_libraries(GLFWPP INTERFACE glfw)
else()
# then try with pkg-config (in case cmake config files are missing for some reason)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GLFW IMPORTED_TARGET glfw3)
if(GLFW_FOUND)
target_link_libraries(GLFWPP INTERFACE glfw)
else()
# then build from source if needed
include(FetchContent)
FetchContent_Declare(
glfw
GIT_REPOSITORY https://github.com/glfw/glfw.git
GIT_TAG 3.3.8)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(glfw)
target_link_libraries(GLFWPP INTERFACE glfw)
endif()
endif()
endif()

#Build examples
if (GLFWPP_BUILD_EXAMPLES)
if (NOT (CMAKE_SYSTEM_NAME STREQUAL "Emscripten"))
add_subdirectory_checked(${CMAKE_CURRENT_SOURCE_DIR}/external/glew-cmake)
include(FetchContent)
FetchContent_Declare(glew-cmake
GIT_REPOSITORY https://github.com/Perlmint/glew-cmake.git
GIT_TAG 7c2b7514f64234756f7df051b9316f1e46f5a6e7)
FetchContent_MakeAvailable(glew-cmake)
endif()
add_subdirectory_checked(${CMAKE_CURRENT_SOURCE_DIR}/external/imgui)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/external/imgui)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/examples)
endif ()
endif ()
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ I like C++ and OOP, so when I find a C library, I immediately look for a wrapper
To use, just clone the repo recursively:

```powershell
git clone https://github.com/janekb04/glfwpp --recurse-submodules
git clone https://github.com/janekb04/glfwpp
```

Remember to install [the necessary GLFW dependencies](https://www.glfw.org/docs/latest/compile.html), if you're on Linux. Make sure to disable building the examples by setting the option `GLFWPP_BUILD_EXAMPLES` to `OFF` using `set(GLFWPP_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)` in your `CMakeLists.txt`, if you don't want them built, as they are built by default. If you don't disable them, you will also have to install [the Vulkan SDK](https://vulkan.lunarg.com).
Expand Down
13 changes: 9 additions & 4 deletions examples/basic.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <GL/glew.h>
#include <cmath>
#include <glfwpp/glfwpp.h>

#include <cmath>
#include <string>

int main()
{
[[maybe_unused]] glfw::GlfwLibrary library = glfw::init();
Expand All @@ -12,17 +14,20 @@ int main()
hints.contextVersionMinor = 6;
hints.apply();
// Or with C++20:
//glfw::WindowHints{
// glfw::WindowHints{
// .clientApi = glfw::ClientApi::OpenGl,
// .contextVersionMajor = 4,
// .contextVersionMinor = 6}
// .apply();
glfw::Window wnd(800, 600, "GLFWPP basic example");

glfw::makeContextCurrent(wnd);
if(glewInit() != GLEW_OK)
GLenum err = glewInit();
if(err != GLEW_OK)
{
throw std::runtime_error("Could not initialize GLEW");
std::string err_string = "Could not initialize GLEW: ";
err_string.append(reinterpret_cast<const char*>(glewGetErrorString(err)));
throw std::runtime_error(err_string);
}

while(!wnd.shouldClose())
Expand Down
1 change: 0 additions & 1 deletion external/glew-cmake
Submodule glew-cmake deleted from 7c2b75
1 change: 0 additions & 1 deletion external/glfw
Submodule glfw deleted from d3ede7
49 changes: 32 additions & 17 deletions external/imgui/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
add_library (Imgui
imgui/imgui.cpp
imgui/imgui_demo.cpp
imgui/imgui_draw.cpp
imgui/imgui_tables.cpp
imgui/imgui_widgets.cpp
imgui/backends/imgui_impl_glfw.cpp
imgui/backends/imgui_impl_opengl3.cpp
)
target_include_directories(Imgui PUBLIC
imgui
imgui/backends
)
target_link_libraries(Imgui PUBLIC glfw libglew_static)
# TODO: search with find pacakge and use wrapper for imgui from vcpkg
include(FetchContent)
FetchContent_Declare(imgui
GIT_REPOSITORY https://github.com/ocornut/imgui.git
GIT_TAG 1e14cc5cae703b7cdf0e33c50165d6dd289fcb1f)

FetchContent_GetProperties(imgui)
if(NOT imgui_POPULATED)
FetchContent_Populate(imgui)

add_library (Imgui
${imgui_SOURCE_DIR}/imgui.cpp
${imgui_SOURCE_DIR}/imgui_demo.cpp
${imgui_SOURCE_DIR}/imgui_draw.cpp
${imgui_SOURCE_DIR}/imgui_tables.cpp
${imgui_SOURCE_DIR}/imgui_widgets.cpp
${imgui_SOURCE_DIR}/backends/imgui_impl_glfw.cpp
${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp
)
target_include_directories(Imgui PUBLIC
${imgui_SOURCE_DIR}
${imgui_SOURCE_DIR}/backends
)
target_link_libraries(Imgui PUBLIC glfw libglew_static)

if (WIN32 AND NOT (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC"))
target_link_libraries(Imgui PUBLIC "Imm32.lib")
endif()
endif()




if (WIN32 AND NOT (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC"))
target_link_libraries(Imgui PUBLIC "Imm32.lib")
endif()
1 change: 0 additions & 1 deletion external/imgui/imgui
Submodule imgui deleted from 1e14cc
3 changes: 2 additions & 1 deletion include/glfwpp/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define GLFWPP_HELPER_H

#include <type_traits>
#include <utility>

#define GLFWPP_ENUM_FLAGS_OPERATORS(Enum) \
inline std::underlying_type_t<Enum> operator~(Enum lhs) \
Expand Down Expand Up @@ -69,4 +70,4 @@ namespace glfw
} // namespace detail
} // namespace glfw

#endif //GLFWPP_HELPER_H
#endif // GLFWPP_HELPER_H