Skip to content

Commit

Permalink
build,refactor: improve cmake script composing (#1)
Browse files Browse the repository at this point in the history
* build,refactor: improve cmake script composing

* ci: install dependencies required for Ubuntu

* chore: add .gitignore

* feat: set fetch depth as 1 when libgit2 version no less than v1.7.0
  • Loading branch information
WhiredPlanck authored Jan 5, 2024
1 parent 83fab3a commit 92d47cc
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 49 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ jobs:
- uses: actions/checkout@v3
with:
submodules: true
- name: Install libgit2 for Ubuntu
- name: Install dependencies for Ubuntu
if: ${{ matrix.os == 'ubuntu-latest' }}
run: |
sudo apt update
sudo apt -y install libgit2-dev
sudo apt -y install libgit2-dev libcxxopts-dev nlohmann-json3-dev libyaml-cpp-dev
- name: Set reusable strings
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/
.cache
111 changes: 65 additions & 46 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,43 +1,58 @@
# cmake version
cmake_minimum_required(VERSION 3.15)

# project name
PROJECT(rppi_get)

# build config
#set(CMAKE_BUILD_TYPE "Release")
#set(CMAKE_BUILD_TYPE "Debug")

# for libgit2
if(MSVC)
set(BUILD_SHARED_LIBS ON CACHE BOOL "Build Shared Library (OFF for Static)" FORCE)
endif()
project(rppi_get)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(BUILD_TESTS OFF CACHE BOOL "Build Tests using the Clar suite" FORCE)
set(BUILD_EXAMPLES OFF CACHE BOOL "Build the example tree.Build library usage example apps" FORCE)
set(BUILD_CLI OFF CACHE BOOL "Build the command-line interface." FORCE)
include(GNUInstallDirs)

# for libgit2 ends
# options
option(USE_BUNDLED_CXXOPTS "Use built-in version of cxxopts" ${MSVC})
option(USE_BUNDLED_JSON "Use built-in version of nlohmann/json" ${MSVC})
option(USE_BUNDLED_LIBGIT2 "Use built-in version of libgit2" ${MSVC})
option(USE_BUNDLED_YAMLCPP "Use built-in version of yaml-cpp" ${MSVC})

add_subdirectory(deps/yaml-cpp)
add_subdirectory(deps/libgit2)
add_subdirectory(deps/cxxopts)
add_subdirectory(deps/json)
# dependencies
if (USE_BUNDLED_CXXOPTS)
add_subdirectory(deps/cxxopts)
else()
find_package(cxxopts REQUIRED)
endif()

set(LIBGIT2_LIB_DIR "${CMAKE_CURRENT_BINARY_DIR}/deps/libgit2")
link_directories(${LIBGIT2_LIB_DIR})
if (USE_BUNDLED_JSON)
set(JSON_BuildTests OFF CACHE INTERNAL "")
add_subdirectory(deps/json)
else()
find_package(nlohmann_json REQUIRED)
endif()

#header path
INCLUDE_DIRECTORIES (
include
deps/cxxopts/include
deps/json/include
deps/libgit2/include
)
if (USE_BUNDLED_LIBGIT2)
set(BUILD_TESTS OFF CACHE INTERNAL "")
set(BUILD_CLI OFF CACHE INTERNAL "")
if (MSVC)
set(BUILD_SHARED_LIBS ON CACHE INTERNAL "")
endif()
add_subdirectory(deps/libgit2)
set(LIBGIT2_LIBRARIES libgit2package)
set(LIBGIT2_INCLUDE_DIR deps/libgit2/include)
else()
find_package(PkgConfig REQUIRED)
pkg_search_module(LIBGIT2 REQUIRED libgit2)
endif()

if (USE_BUNDLED_YAMLCPP)
set(YAML_CPP_BUILD_TOOLS OFF CACHE INTERNAL "")
set(YAML_CPP_BUILD_TESTS OFF CACHE INTERNAL "")
set(YAML_CPP_INSTALL OFF CACHE INTERNAL "")
add_subdirectory(deps/yaml-cpp)
else()
find_package(yaml-cpp REQUIRED)
endif()

if (WIN32)
set(WIN32_LIBS shlwapi user32 Rpcrt4)
endif()

# source directory
aux_source_directory(./src DIR_SRC)
Expand All @@ -48,24 +63,28 @@ set(rppi_get ${DIR_SRC})
# add executable
add_executable(${PROJECT_NAME} ${rppi_get})

# add link library
if(WIN32)
target_link_libraries( ${PROJECT_NAME} PRIVATE git2 yaml-cpp shlwapi user32 Rpcrt4)
else()
target_link_libraries( ${PROJECT_NAME} PRIVATE git2 yaml-cpp)
endif()
# add link libraries
target_link_libraries(${PROJECT_NAME}
cxxopts::cxxopts
${LIBGIT2_LIBRARIES}
nlohmann_json::nlohmann_json
yaml-cpp
${WIN32_LIBS}
)

set(TARGET_FILES
${CMAKE_SOURCE_DIR}/rppi_config.yaml
)
# add include directories
target_include_directories(${PROJECT_NAME} PUBLIC
${LIBGIT2_INCLUDE_DIR}
)

set(INSTALL_DIR ${CMAKE_SOURCE_DIR}/dist)
set(TARGET_FILES ${CMAKE_SOURCE_DIR}/rppi_config.yaml)

install(
TARGETS ${PROJECT_NAME}
DESTINATION ${INSTALL_DIR}
)
TARGETS ${PROJECT_NAME}
DESTINATION ${CMAKE_INSTALL_BINDIR}
)

install(
FILES ${TARGET_FILES}
DESTINATION ${INSTALL_DIR}
)
FILES ${TARGET_FILES}
DESTINATION ${CMAKE_INSTALL_BINDIR}
)
11 changes: 11 additions & 0 deletions src/git.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <git2.h>

#ifdef LIBGIT2_VER_MINOR
# define CHECK_LIBGIT2_VERSION(MAJOR, MINOR) \
((LIBGIT2_VER_MAJOR == (MAJOR) && LIBGIT2_VER_MINOR >= (MINOR)) || \
LIBGIT2_VER_MAJOR > (MAJOR))
#else /* ! defined(LIBGIT2_VER_MINOR) */
# define CHECK_LIBGIT2_VERSION(MAJOR, MINOR) 0
#endif
4 changes: 3 additions & 1 deletion src/rppi_get.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include <cxxopts.hpp>
#include <filesystem>
#include <fstream>
#include <git2.h>
#include <iostream>
#include <nlohmann/json.hpp>
#include <regex>
#include <stdio.h>
#include <string>
#include <vector>
#include <yaml-cpp/yaml.h>
#include "git.hpp"
#ifdef _WIN32
#include <Windows.h>
#include <rpcdce.h>
Expand Down Expand Up @@ -338,7 +338,9 @@ int clone_repository(const char *repo_url, const char *local_path,
clone_opts.fetch_opts.proxy_opts.url = proxy_opts;
clone_opts.fetch_opts.callbacks.transfer_progress = transfer_progress;
clone_opts.fetch_opts.callbacks.credentials = credentials_callback;
#if CHECK_LIBGIT2_VERSION(1, 7)
clone_opts.fetch_opts.depth = 1;
#endif
int error = git_clone(&repo, repo_url, local_path, &clone_opts);
if (error != 0 && error != GIT_EEXISTS) {
const git_error *git_error = giterr_last();
Expand Down

0 comments on commit 92d47cc

Please sign in to comment.