Skip to content

Commit 0b92764

Browse files
committed
Rewrite CMake config and install script
1 parent 534c639 commit 0b92764

2 files changed

Lines changed: 100 additions & 92 deletions

File tree

CMakeLists.txt

Lines changed: 95 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,109 @@
11
cmake_minimum_required(VERSION 3.16.3)
2-
set (CMAKE_CXX_STANDARD 14)
2+
project(analysis_tools_ts C CXX)
33

4-
project(analysis_tools_ts)
4+
set(CMAKE_CXX_STANDARD 14)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
56

6-
set(JSONCODE include/json/include/)
7+
include(FetchContent)
78

8-
if(JSONDIR)
9-
set(JSONCODE ${JSONDIR})
9+
FetchContent_Declare(
10+
tree-sitter
11+
GIT_REPOSITORY https://github.com/tree-sitter/tree-sitter.git
12+
GIT_TAG 0535b0ca378a3f13a2b469f6c090a0c1b90904b7
13+
GIT_SHALLOW TRUE
14+
)
15+
16+
FetchContent_GetProperties(tree-sitter)
17+
if(NOT tree-sitter_POPULATED)
18+
FetchContent_Populate(tree-sitter)
19+
20+
# Build the core library from the fetched source
21+
add_library(tree-sitter STATIC ${tree-sitter_SOURCE_DIR}/lib/src/lib.c)
22+
target_include_directories(tree-sitter PUBLIC ${tree-sitter_SOURCE_DIR}/lib/include)
1023
endif()
1124

12-
add_library(tree-sitter-python include/tree-sitter-python/src/parser.c
13-
include/tree-sitter-python/src/scanner.c)
25+
function(fetch_and_build_ts_parser PARSER_NAME GIT_URL GIT_COMMIT)
26+
FetchContent_Declare(
27+
${PARSER_NAME}
28+
GIT_REPOSITORY ${GIT_URL}
29+
GIT_TAG ${GIT_COMMIT}
30+
GIT_SHALLOW TRUE
31+
)
32+
FetchContent_GetProperties(${PARSER_NAME})
33+
if(NOT ${PARSER_NAME}_POPULATED)
34+
FetchContent_Populate(${PARSER_NAME})
35+
36+
# Collect sources
37+
set(SOURCES ${${PARSER_NAME}_SOURCE_DIR}/src/parser.c)
38+
if(EXISTS ${${PARSER_NAME}_SOURCE_DIR}/src/scanner.c)
39+
list(APPEND SOURCES ${${PARSER_NAME}_SOURCE_DIR}/src/scanner.c)
40+
elseif(EXISTS ${${PARSER_NAME}_SOURCE_DIR}/src/scanner.cc)
41+
list(APPEND SOURCES ${${PARSER_NAME}_SOURCE_DIR}/src/scanner.cc)
42+
endif()
43+
44+
add_library(${PARSER_NAME} STATIC ${SOURCES})
45+
target_include_directories(${PARSER_NAME} PRIVATE ${${PARSER_NAME}_SOURCE_DIR}/src)
46+
target_link_libraries(${PARSER_NAME} PRIVATE tree-sitter)
47+
endif()
48+
endfunction()
49+
50+
# Fetch each language. Uses the latest commit as of 03-29-2026
1451

15-
add_library(tree-sitter-cpp include/tree-sitter-cpp/src/parser.c
16-
include/tree-sitter-cpp/src/scanner.c)
52+
fetch_and_build_ts_parser(
53+
tree-sitter-python
54+
https://github.com/tree-sitter/tree-sitter-python.git
55+
26855eabccb19c6abf499fbc5b8dc7cc9ab8bc64
56+
)
1757

18-
add_library(tree-sitter-c include/tree-sitter-c/src/parser.c)
58+
fetch_and_build_ts_parser(
59+
tree-sitter-c
60+
https://github.com/tree-sitter/tree-sitter-c.git
61+
ae19b676b13bdcc13b7665397e6d9b14975473dd
62+
)
1963

20-
add_library(tree-sitter-java include/tree-sitter-java/src/parser.c)
64+
fetch_and_build_ts_parser(
65+
tree-sitter-cpp
66+
https://github.com/tree-sitter/tree-sitter-cpp.git
67+
8b5b49eb196bec7040441bee33b2c9a4838d696
68+
)
2169

22-
link_libraries(tree-sitter-python tree-sitter-c tree-sitter-cpp tree-sitter-java
23-
${PROJECT_SOURCE_DIR}/include/tree-sitter/libtree-sitter.a)
70+
fetch_and_build_ts_parser(
71+
tree-sitter-java
72+
https://github.com/tree-sitter/tree-sitter-java.git
73+
e10607b45ff745f5f876bfa3e94fbcc6b44bdc11
74+
)
2475

25-
add_executable(submitty_count_ts src/count.cpp src/parser.cpp src/utils.cpp)
76+
FetchContent_Declare(
77+
nlohmann_json
78+
GIT_REPOSITORY https://github.com/nlohmann/json.git
79+
GIT_TAG 9a737481aed085fd289f82dff1fa8c3c66627a7e
80+
GIT_SHALLOW TRUE
81+
)
82+
FetchContent_MakeAvailable(nlohmann_json)
2683

27-
add_executable(submitty_diagnostics_ts src/diagnostics.cpp src/parser.cpp
28-
src/utils.cpp)
84+
set(COMMON_LIBS
85+
tree-sitter
86+
tree-sitter-python
87+
tree-sitter-c
88+
tree-sitter-cpp
89+
tree-sitter-java
90+
)
2991

30-
include_directories(include/tree-sitter/lib/include)
92+
# Submitty Count
93+
add_executable(submitty_count_ts
94+
src/count.cpp
95+
src/parser.cpp
96+
src/utils.cpp
97+
)
98+
target_link_libraries(submitty_count_ts PRIVATE ${COMMON_LIBS})
3199

32-
target_include_directories(submitty_diagnostics_ts PUBLIC ${JSONCODE})
100+
# Submitty Diagnostics
101+
add_executable(submitty_diagnostics_ts
102+
src/diagnostics.cpp
103+
src/parser.cpp
104+
src/utils.cpp
105+
)
106+
target_link_libraries(submitty_diagnostics_ts PRIVATE
107+
${COMMON_LIBS}
108+
nlohmann_json::nlohmann_json
109+
)

install_analysistoolsts.sh

Lines changed: 5 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
set -e
44

5-
########################################################################################################################
6-
########################################################################################################################
7-
# this script must be run by root or sudo
85
if [[ "$UID" -ne "0" ]] ; then
9-
echo "ERROR: This script must be run by root or sudo"
6+
echo "ERROR: This script must be run as root"
107
exit 1
118
fi
129

@@ -19,85 +16,19 @@ fi
1916
source "${VARS}"
2017

2118
echo -e "Installing AnalysisToolsTS... "
22-
2319
mkdir -p "${INSTALLATION_DIR}"
2420

2521
# Copy cloned files to AnalysisToolsTS directory
2622
if [ $# -eq 0 ]; then
2723
rsync -rtz "${REPO_DIR}/src" "${REPO_DIR}/CMakeLists.txt" "${INSTALLATION_DIR}"
2824
fi
2925

30-
mkdir -p "${INCLUDE_DIR}"
31-
32-
########################################################################
33-
34-
# Clone the tree-sitter repos
35-
repos=( tree-sitter tree-sitter-python tree-sitter-c tree-sitter-cpp tree-sitter-java)
36-
37-
for repo in "${repos[@]}"
38-
do
39-
dir="${INCLUDE_DIR}"/"${repo}"
40-
41-
echo "clone or update ${repo}... "
42-
43-
# TEMPORARY - workaround due to problem with the git repository
44-
# just delete the directory and start over
45-
echo "removing ${dir}"
46-
rm -rf "${dir}"
47-
# TEMPORARY
48-
49-
if [ -d "${dir}" ]; then
50-
echo "pulling changes ..."
51-
# IF THE REPO ALREADY EXISTS...
52-
pushd "${dir}"
53-
54-
CURRENT_BRANCH=$(git branch --show-current)
55-
56-
# PULL CHANGES
57-
git fetch
58-
git reset --hard HEAD
59-
git merge origin/"${CURRENT_BRANCH}"
60-
popd
61-
62-
else
63-
# THE REPO DID NOT EXIST
64-
echo "the repository did not previously exist cloning... "
65-
pushd "${INCLUDE_DIR}"
66-
git clone --depth 1 "https://github.com/tree-sitter/${repo}"
67-
popd
68-
69-
fi
70-
done
71-
72-
# CHECKOUT & INSTALL THE NLOHMANN C++ JSON LIBRARY
73-
# If we don't already have a copy of this repository, check it out, only for local development
74-
if [ $# -gt 0 ] && [ ! -d "${NLOHMANN_DIR}" ]; then
75-
git clone --depth 1 "https://github.com/nlohmann/json.git" "${NLOHMANN_DIR}"
76-
fi
77-
78-
########################################################################
79-
80-
# build tree sitter library
81-
pushd "${INCLUDE_DIR}"/tree-sitter
82-
83-
make
84-
85-
popd
86-
87-
echo "building submitty_count_ts ..."
88-
89-
# Compile the project
90-
mkdir -p "${INSTALLATION_DIR}/build"
91-
92-
cmake -S "${INSTALLATION_DIR}" -B "${INSTALLATION_DIR}/build" -DJSONDIR="${NLOHMANN_INCLUDE_DIR}"
93-
94-
pushd "${INSTALLATION_DIR}/build"
95-
96-
make
26+
BUILD_DIR="${INSTALLATION_DIR}/build"
27+
mkdir -p "${BUILD_DIR}"
9728

98-
popd
29+
cmake -S "${INSTALLATION_DIR}" -B "${BUILD_DIR}"
30+
cmake --build "${BUILD_DIR}" --parallel "$(nproc)"
9931

100-
# # change permissions
10132
if [ $# -eq 0 ]; then
10233
chown -R root:root "${INSTALLATION_DIR}"
10334
chmod -R 755 "${INSTALLATION_DIR}"

0 commit comments

Comments
 (0)