Skip to content

Commit b152576

Browse files
authored
make minja packagable by setting target in cmake (#62)
* make minja packagable by setting target in cmake * preventing gtest install * Add cmake instruct in README
1 parent fcb5a0d commit b152576

File tree

12 files changed

+84
-50
lines changed

12 files changed

+84
-50
lines changed

CMakeLists.txt

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ cmake_policy(SET CMP0135 NEW) # https://cmake.org/cmake/help/latest/policy/CMP01
1111

1212
project(minja VERSION 1.0.0 LANGUAGES CXX)
1313

14+
add_library(minja INTERFACE)
15+
1416
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1517

1618
# Test if clang-tidy is available
@@ -36,6 +38,8 @@ else()
3638
set(MINJA_FUZZTEST_ENABLED_DEFAULT ON)
3739
set(MINJA_USE_VENV_DEFAULT ON)
3840
endif()
41+
option(MINJA_TEST_ENABLED "minja: Build with test(python interpreter required)" ON)
42+
option(MINJA_EXAMPLE_ENABLED "minja: Build with example" ON)
3943
option(MINJA_FUZZTEST_ENABLED "minja: fuzztests enabled" MINJA_FUZZTEST_ENABLED_DEFAULT)
4044
option(MINJA_FUZZTEST_FUZZING_MODE "minja: run fuzztests (if enabled) in fuzzing mode" OFF)
4145
option(MINJA_USE_VENV "minja: use Python venv for build" MINJA_USE_VENV_DEFAULT)
@@ -53,15 +57,20 @@ include(FetchContent)
5357
# Fetch nlohmann/json
5458
FetchContent_Declare(json URL https://github.com/nlohmann/json/archive/refs/heads/develop.zip)
5559
FetchContent_MakeAvailable(json)
56-
57-
if (MINJA_FUZZTEST_ENABLED)
58-
# Fetch google/fuzztest (and indirectly, gtest)
59-
FetchContent_Declare(fuzztest URL https://github.com/google/fuzztest/archive/refs/heads/main.zip)
60-
FetchContent_MakeAvailable(fuzztest)
61-
else()
62-
# Fetch gtest
63-
FetchContent_Declare(googletest URL https://github.com/google/googletest/archive/refs/heads/main.zip)
64-
FetchContent_MakeAvailable(googletest)
60+
target_link_libraries(minja INTERFACE nlohmann_json::nlohmann_json)
61+
62+
if(MINJA_TEST_ENABLED)
63+
if (MINJA_FUZZTEST_ENABLED)
64+
# Fetch google/fuzztest (and indirectly, gtest)
65+
FetchContent_Declare(fuzztest URL https://github.com/google/fuzztest/archive/refs/heads/main.zip)
66+
FetchContent_MakeAvailable(fuzztest)
67+
message(STATUS "${fuzztest_BINARY_DIR}: ${${fuzztest_BINARY_DIR}}")
68+
else()
69+
# Fetch gtest
70+
set(INSTALL_GTEST OFF)
71+
FetchContent_Declare(googletest URL https://github.com/google/googletest/archive/refs/heads/main.zip)
72+
FetchContent_MakeAvailable(googletest)
73+
endif()
6574
endif()
6675

6776
# Use ccache if installed
@@ -77,40 +86,57 @@ if (NOT XCODE AND NOT MSVC AND NOT CMAKE_BUILD_TYPE)
7786
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
7887
endif()
7988

80-
set(Python_FIND_STRATEGY LOCATION CACHE STRING "Python find strategy" FORCE)
81-
find_package(Python COMPONENTS Interpreter REQUIRED)
82-
if(MINJA_USE_VENV)
83-
# Create a python venv w/ the required dependencies
84-
set(VENV_DIR "${CMAKE_BINARY_DIR}/venv")
85-
if(WIN32)
86-
set(VENV_PYTHON "${VENV_DIR}/Scripts/python.exe")
87-
else()
88-
set(VENV_PYTHON "${VENV_DIR}/bin/python")
89+
if(MINJA_TEST_ENABLED)
90+
set(Python_FIND_STRATEGY LOCATION CACHE STRING "Python find strategy" FORCE)
91+
find_package(Python COMPONENTS Interpreter REQUIRED)
92+
if(MINJA_USE_VENV)
93+
# Create a python venv w/ the required dependencies
94+
set(VENV_DIR "${CMAKE_BINARY_DIR}/venv")
95+
if(WIN32)
96+
set(VENV_PYTHON "${VENV_DIR}/Scripts/python.exe")
97+
else()
98+
set(VENV_PYTHON "${VENV_DIR}/bin/python")
99+
endif()
100+
execute_process(
101+
COMMAND ${Python_EXECUTABLE} -m venv "${VENV_DIR}"
102+
COMMAND_ERROR_IS_FATAL ANY)
103+
execute_process(
104+
COMMAND ${VENV_PYTHON} -m pip install -r "${CMAKE_SOURCE_DIR}/requirements.txt"
105+
COMMAND_ERROR_IS_FATAL ANY)
106+
set(Python_EXECUTABLE "${VENV_PYTHON}" CACHE FILEPATH "Path to Python executable in venv" FORCE)
89107
endif()
90-
execute_process(
91-
COMMAND ${Python_EXECUTABLE} -m venv "${VENV_DIR}"
92-
COMMAND_ERROR_IS_FATAL ANY)
93-
execute_process(
94-
COMMAND ${VENV_PYTHON} -m pip install -r "${CMAKE_SOURCE_DIR}/requirements.txt"
95-
COMMAND_ERROR_IS_FATAL ANY)
96-
set(Python_EXECUTABLE "${VENV_PYTHON}" CACHE FILEPATH "Path to Python executable in venv" FORCE)
108+
message(STATUS "Python executable: ${Python_EXECUTABLE}")
97109
endif()
98-
message(STATUS "Python executable: ${Python_EXECUTABLE}")
99110

100111
find_program(CPPCHECK cppcheck)
101112
if(CPPCHECK)
102113
set(CMAKE_CXX_CPPCHECK "${CPPCHECK}" -i ${json_SOURCE_DIR}/include/nlohmann/json.hpp)
103114
message(STATUS "cppcheck found: ${CPPCHECK}")
104115
endif()
105116

106-
message(STATUS "${fuzztest_BINARY_DIR}: ${${fuzztest_BINARY_DIR}}")
107-
include_directories(
108-
include/minja
109-
${json_SOURCE_DIR}/include
117+
include(GNUInstallDirs)
118+
target_include_directories(minja INTERFACE
119+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
120+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
110121
)
111122

112-
add_subdirectory(examples)
123+
install(FILES
124+
${PROJECT_SOURCE_DIR}/include/minja/minja.hpp
125+
${PROJECT_SOURCE_DIR}/include/minja/chat-template.hpp
126+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/minja
127+
)
128+
install(
129+
TARGETS minja
130+
EXPORT "${TARGETS_EXPORT_NAME}"
131+
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/minja # for downstream projects
132+
)
113133

114-
enable_testing()
115-
include(GoogleTest)
116-
add_subdirectory(tests)
134+
if(MINJA_EXAMPLE_ENABLED)
135+
add_subdirectory(examples)
136+
endif()
137+
138+
if(MINJA_TEST_ENABLED)
139+
enable_testing()
140+
include(GoogleTest)
141+
add_subdirectory(tests)
142+
endif()

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ It is **not general purpose**: it includes just what’s needed for actual chat
3333

3434
This library is header-only: just copy the header(s) you need, make sure to use a compiler that handles C++17 and you're done. Oh, and get [nlohmann::json](https://github.com/nlohmann/json) in your include path.
3535

36+
If your project is based on [cmake](https://cmake.org/), can simply import by using `FetchContent`.
37+
```
38+
FetchContent_Declare(minja GIT_REPOSITORY "https://github.com/google/minja")
39+
FetchContent_MakeAvailable(minja)
40+
41+
target_link_libraries(<YOUR_TARGET> PRIVATE minja)
42+
```
43+
3644
See API in [minja/minja.hpp](./include/minja/minja.hpp) and [minja/chat-template.hpp](./include/minja/chat-template.hpp) (experimental).
3745

3846
For raw Jinja templating (see [examples/raw.cpp](./examples/raw.cpp)):

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ foreach(example
1111
)
1212
add_executable(${example} ${example}.cpp)
1313
target_compile_features(${example} PUBLIC cxx_std_17)
14-
target_link_libraries(${example} PRIVATE nlohmann_json::nlohmann_json)
14+
target_link_libraries(${example} PRIVATE minja)
1515
if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
1616
target_compile_definitions(${example} PUBLIC _CRT_SECURE_NO_WARNINGS)
1717
endif()

examples/chat-template.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
https://opensource.org/licenses/MIT.
77
*/
88
// SPDX-License-Identifier: MIT
9-
#include <chat-template.hpp>
9+
#include <minja/chat-template.hpp>
1010
#include <iostream>
1111

1212
using json = nlohmann::ordered_json;

examples/raw.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
https://opensource.org/licenses/MIT.
77
*/
88
// SPDX-License-Identifier: MIT
9-
#include <minja.hpp>
9+
#include <minja/minja.hpp>
1010
#include <iostream>
1111

1212
using json = nlohmann::ordered_json;

tests/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "ar
1313
target_compile_options(gtest PRIVATE -Wno-language-extension-token)
1414
endif()
1515
target_link_libraries(test-syntax PRIVATE
16-
nlohmann_json::nlohmann_json
16+
minja
1717
gtest_main
1818
gmock
1919
)
@@ -28,7 +28,7 @@ else()
2828
target_compile_options(gtest PRIVATE -Wno-language-extension-token)
2929
endif()
3030
target_link_libraries(test-chat-template PRIVATE
31-
nlohmann_json::nlohmann_json
31+
minja
3232
gtest_main
3333
gmock
3434
)
@@ -41,7 +41,7 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "ar
4141
target_compile_options(gtest PRIVATE -Wno-language-extension-token)
4242
endif()
4343
target_link_libraries(test-polyfills PRIVATE
44-
nlohmann_json::nlohmann_json
44+
minja
4545
gtest_main
4646
gmock
4747
)
@@ -61,7 +61,7 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "ar
6161
target_compile_options(gtest PRIVATE -Wno-language-extension-token)
6262
endif()
6363
target_link_libraries(test-capabilities PRIVATE
64-
nlohmann_json::nlohmann_json
64+
minja
6565
gtest_main
6666
gmock
6767
)
@@ -77,7 +77,7 @@ target_compile_features(test-supported-template PUBLIC cxx_std_17)
7777
if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
7878
target_compile_definitions(test-supported-template PUBLIC _CRT_SECURE_NO_WARNINGS)
7979
endif()
80-
target_link_libraries(test-supported-template PRIVATE nlohmann_json::nlohmann_json)
80+
target_link_libraries(test-supported-template PRIVATE minja)
8181

8282
# https://huggingface.co/models?other=conversational
8383
# https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard#/?types=fine-tuned%2Cchat

tests/test-capabilities.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
https://opensource.org/licenses/MIT.
77
*/
88
// SPDX-License-Identifier: MIT
9-
#include "chat-template.hpp"
9+
#include "minja/chat-template.hpp"
1010

1111
#include <gtest/gtest.h>
1212
#include <gmock/gmock-matchers.h>

tests/test-chat-template.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
https://opensource.org/licenses/MIT.
88
*/
99
// SPDX-License-Identifier: MIT
10-
#include "chat-template.hpp"
10+
#include "minja/chat-template.hpp"
1111
#include "gtest/gtest.h"
1212
#include <gtest/gtest.h>
1313
#include <gmock/gmock-matchers.h>

tests/test-fuzz.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include <fuzztest/fuzztest.h>
1010
#include <fuzztest/grammars/json_grammar.h>
1111
#include <gtest/gtest.h>
12-
#include <minja.hpp>
13-
#include <chat-template.hpp>
12+
#include <minja/minja.hpp>
13+
#include <minja/chat-template.hpp>
1414
#include <iostream>
1515
#include <sstream>
1616
#include <stdexcept>

tests/test-polyfills.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
https://opensource.org/licenses/MIT.
77
*/
88
// SPDX-License-Identifier: MIT
9-
#include "minja.hpp"
9+
#include "minja/minja.hpp"
1010
#include <gtest/gtest.h>
1111
#include <gmock/gmock-matchers.h>
1212

1313
#include <fstream>
1414
#include <iostream>
1515
#include <string>
16-
#include "chat-template.hpp"
16+
#include "minja/chat-template.hpp"
1717

1818
using namespace minja;
1919

0 commit comments

Comments
 (0)