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

CMake build and test. #303

Merged
merged 12 commits into from
Oct 11, 2017
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
**/.*.swp
**/.*.swo
**/*.o
**/*.a
**/*.dylib
*.dSYM

jsonnet
Expand All @@ -27,6 +29,21 @@ bazel-*
**/*.tfstate.backup

build/
external/
dist/
jsonnet.egg-info/

# Cmake
**/CMakeCache.txt
**/CMakeFiles
**/cmake_install.cmake
**/CTestTestfile.cmake
tags
bin/
Testing/

# Ignore auto-generated makefiles from CMake.
**/Makefile
^Makefile/

**/.DS_Store
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ script: |
make test
python setup.py build
python setup.py test
cmake . -Bbuild && cmake --build build --target run_tests
branches:
only:
- master
Expand Down
105 changes: 105 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Travis CI runs CMake 2.8.7 so we are pinned to that verison.
cmake_minimum_required(VERSION 2.8.7)
include(ExternalProject)

# User-configurable options.
option(BUILD_JSONNET "Build jsonnet command-line tool." ON)
option(BUILD_TESTS "Build and run jsonnet tests." ON)
set(GLOBAL_OUTPUT_PATH_SUFFIX "bin" CACHE STRING
"Output artifacts directory.")

project(jsonnet)

# Discourage in-source builds because they overwrite the hand-written Makefile.
# Use `cmake . -B<dir>` or the CMake GUI to do an out-of-source build.
if (${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR} AND
${CMAKE_GENERATOR} MATCHES "Makefile")
message(WARNING "In-source builds with the a makefile generator overwrite the handwritten Makefile. Out-of-source builds are recommended for this project.")
endif()


# Disable CMake >3.0 warnings.
set(CMAKE_MACOSX_RPATH 1)

# Set output paths.
set(GLOBAL_OUTPUT_PATH "${PROJECT_BINARY_DIR}/${GLOBAL_OUTPUT_PATH_SUFFIX}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${GLOBAL_OUTPUT_PATH})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${GLOBAL_OUTPUT_PATH})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${GLOBAL_OUTPUT_PATH})

# Include external googletest project.
if (BUILD_TESTS)
enable_testing()

# TODO: Support manual googletest path.
# Download googletest library source.
set(GOOGLETEST_OUTPUT_DIR ${GLOBAL_OUTPUT_PATH}/googletest)
ExternalProject_Add(
googletest

GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG "release-1.8.0"

UPDATE_COMMAND ""
PATCH_COMMAND ""
TEST_COMMAND ""

PREFIX ${PROJECT_SOURCE_DIR}/external/googletest
CMAKE_ARGS -DBUILD_GTEST=ON -DBUILD_GMOCK=OFF
-DCMAKE_INSTALL_PREFIX=${GOOGLETEST_OUTPUT_DIR}
)

# Copy googletest build outputs to global output path.
ExternalProject_Add_Step(
googletest copy_to_bin
COMMAND ${CMAKE_COMMAND} -E copy_directory
${GOOGLETEST_OUTPUT_DIR}/lib ${GLOBAL_OUTPUT_PATH}
DEPENDEES install
)

include_directories(${GOOGLETEST_OUTPUT_DIR}/include)
set(
GTEST_LIBRARY
${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
)
set(
GTEST_MAIN_LIBRARY
${CMAKE_STATIC_LIBRARY_PREFIX}gtest_main${CMAKE_STATIC_LIBRARY_SUFFIX}
)
endif()

# Compiler flags.
if (${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" OR
${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -Wextra -pedantic -std=c99")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -Wextra -Woverloaded-virtual -pedantic -std=c++0x -fPIC")
else()
# TODO: Windows support.
message(FATAL_ERROR "Compiler ${CMAKE_CXX_COMPILER_ID} not supported")
endif()

# Look for libraries in global output path.
link_directories(${GLOBAL_OUTPUT_PATH})

# Targets

include_directories(
include
third_party/md5
core)

install(DIRECTORY include DESTINATION include)

add_subdirectory(third_party/md5)
add_subdirectory(core)
add_subdirectory(cmd)

if (BUILD_TESTS)
# |run_tests| target builds and runs all tests. The cmake-generated |test|
# target runs tests without building them.
add_custom_target(run_tests COMMAND ${CMAKE_CTEST_COMMAND}
DEPENDS libjsonnet_test libjsonnet_test_file libjsonnet_test_snippet
jsonnet parser_test lexer_test
)

endif()
9 changes: 9 additions & 0 deletions cmd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Jsonnet command-line tool.

if (BUILD_JSONNET OR BUILD_TESTS)
add_executable(jsonnet ${LIBJSONNET_SOURCE} jsonnet.cpp)
add_dependencies(jsonnet libjsonnet_static)
target_link_libraries(jsonnet libjsonnet_static)

install(TARGETS jsonnet DESTINATION bin)
endif()
67 changes: 67 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# libjsonnet

# Remember to update Bazel and Makefile builds when updating this list!
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and MANIFEST.in and setup.py :(

set(LIBJSONNET_SOURCE
desugarer.cpp
formatter.cpp
lexer.cpp
libjsonnet.cpp
parser.cpp
pass.cpp
static_analysis.cpp
string_utils.cpp
vm.cpp)

add_library(libjsonnet SHARED ${LIBJSONNET_SOURCE})
add_dependencies(libjsonnet md5)
target_link_libraries(libjsonnet md5)

# CMake prepends CMAKE_SHARED_LIBRARY_PREFIX to shared libraries, so without
# this step the output would be |liblibjsonnet|.
set_target_properties(libjsonnet PROPERTIES OUTPUT_NAME jsonnet)
install(TARGETS libjsonnet DESTINATION lib)

# Static library for jsonnet command-line tool.
add_library(libjsonnet_static STATIC ${LIBJSONNET_SOURCE})
add_dependencies(libjsonnet_static md5)
target_link_libraries(libjsonnet_static md5)
set_target_properties(libjsonnet_static PROPERTIES OUTPUT_NAME jsonnet)

# Tests

function(add_test_executable test_name)
if (EXISTS ${CMAKE_CURRENT_LIST_DIR}/${test_name}.cpp)
set(TEST_EXT cpp)
else()
set(TEST_EXT c)
endif()
add_executable(${test_name} ${test_name}.${TEST_EXT})

add_dependencies(${test_name} libjsonnet_static googletest)
target_link_libraries(
${test_name} ${GTEST_LIBRARY} ${GTEST_MAIN_LIBRARY} libjsonnet_static)
endfunction()

if (BUILD_TESTS)
add_test_executable(lexer_test)
add_test(lexer_test ${GLOBAL_OUTPUT_PATH}/lexer_test)

add_test_executable(parser_test)
add_test(parser_test ${GLOBAL_OUTPUT_PATH}/parser_test)

add_test_executable(libjsonnet_test)
add_test(libjsonnet_test ${GLOBAL_OUTPUT_PATH}/libjsonnet_test)

add_test_executable(libjsonnet_test_file)
add_test(libjsonnet_test_file
${GLOBAL_OUTPUT_PATH}/libjsonnet_test_file
${CMAKE_SOURCE_DIR}/test_suite/object.jsonnet)

set(TEST_SNIPPET "std.assertEqual(({ x: 1, y: self.x } { x: 2 }).y, 2)")
add_test_executable(libjsonnet_test_snippet)
add_test(libjsonnet_test_snippet
${GLOBAL_OUTPUT_PATH}/libjsonnet_test_snippet ${TEST_SNIPPET})

add_test(jsonnet_test_snippet
${GLOBAL_OUTPUT_PATH}/jsonnet -e ${TEST_SNIPPET})
endif()
5 changes: 5 additions & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(LIB_HEADER
${LIB_HEADER}
${CMAKE_CURRENT_SOURCE_DIR}/libjsonnet.h
${CMAKE_CURRENT_SOURCE_DIR}/libjsonnet++.h
PARENT_SCOPE)
1 change: 1 addition & 0 deletions third_party/md5/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_library(md5 STATIC md5.cpp)