Skip to content

Commit

Permalink
setup linters and formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
rkaminsk committed Sep 28, 2023
1 parent 9264b34 commit 7c61308
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 419 deletions.
5 changes: 5 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
IndentWidth: '4'
ColumnLimit: '120'
IndentCaseLabels: 'true'
...
387 changes: 2 additions & 385 deletions .clang-tidy

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ACTIVATE=/mnt/scratch/kaminski/conda/bin/activate
if [[ -e "${ACTIVATE}" ]]; then
source /mnt/scratch/kaminski/conda/bin/activate clang
fi
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fail_fast: false
repos:
- repo: https://github.com/pocc/pre-commit-hooks
rev: v1.3.5
hooks:
- id: clang-format
exclude: _clingodl.c
args: ["-i"]
- id: clang-tidy
exclude: _clingodl.c
args: ["-fix"]
25 changes: 20 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
BUILD_TYPE:=debug
CLINGO_DIR:=${HOME}/.local/opt/potassco/$(BUILD_TYPE)/lib/cmake/Clingo
CLANG_TIDY:=clang-tidy;--config-file;$(CURDIR)/.clang-tidy
CXXFLAGS=-Wall -Wextra -Wpedantic -Werror
define cmake_options
-G Ninja \
-S . \
-B "build/$(BUILD_TYPE)" \
-DCMAKE_INSTALL_PREFIX=${HOME}/.local/opt/potassco/$(BUILD_TYPE) \
-DCMAKE_CXX_FLAGS="$(CXXFLAGS)" \
-DClingo_DIR="$(CLINGO_DIR)" \
-DCLINGOLPX_BUILD_TESTS=On \
-DCMAKE_EXPORT_COMPILE_COMMANDS=On
endef

ifeq ($(BUILD_TYPE),profile)
cmake_options += -DCMAKE_BUILD_TYPE="RelWithDebInfo" -DCLINGOLPX_PROFILE=On
else
cmake_options += -DCMAKE_BUILD_TYPE="$(BUILD_TYPE)"
endif


.PHONY: all configure compdb

all: configure
@TERM=dumb MAKEFLAGS= MFLAGS= cmake --build "build/$(BUILD_TYPE)" --target all

test: all
@cmake --build "build/$(BUILD_TYPE)" --target "test"

@TERM=dumb MAKEFLAGS= MFLAGS= cmake --build "build/$(BUILD_TYPE)" --target "test"

%: configure
@cmake --build "build/$(BUILD_TYPE)" --target "$@"
@TERM=dumb MAKEFLAGS= MFLAGS= cmake --build "build/$(BUILD_TYPE)" --target "$@"

# compdb can be installed with pip
compdb: configure
Expand All @@ -22,7 +37,7 @@ compdb: configure
configure: build/$(BUILD_TYPE)/build.ninja

build/$(BUILD_TYPE)/build.ninja:
cmake -G Ninja -H. -B"build/$(BUILD_TYPE)" -DCMAKE_INSTALL_PREFIX=${HOME}/.local/opt/potassco/$(BUILD_TYPE) -DCMAKE_CXX_FLAGS="$(CXXFLAGS)" -DCMAKE_BUILD_TYPE="$(BUILD_TYPE)" -DCMAKE_CXX_CLANG_TIDY:STRING="$(CLANG_TIDY)" -DClingo_DIR="$(CLINGO_DIR)" -DCLINGOLPX_BUILD_TESTS=On -DCMAKE_EXPORT_COMPILE_COMMANDS=On
cmake $(cmake_options)

Makefile:
:
81 changes: 71 additions & 10 deletions cmake/FindGperftools.cmake
Original file line number Diff line number Diff line change
@@ -1,15 +1,76 @@
find_library(GPERFTOOLS_PROFILER NAMES profiler)
# Try to find gperftools
# Once done, this will define
#
# Gperftools_FOUND - system has Profiler
# GPERFTOOLS_INCLUDE_DIR - the Profiler include directories
# Tcmalloc_INCLUDE_DIR - where to find Tcmalloc.h
# GPERFTOOLS_TCMALLOC_LIBRARY - link it to use tcmalloc
# GPERFTOOLS_TCMALLOC_MINIMAL_LIBRARY - link it to use tcmalloc_minimal
# GPERFTOOLS_PROFILER_LIBRARY - link it to use Profiler
# TCMALLOC_VERSION_STRING
# TCMALLOC_VERSION_MAJOR
# TCMALLOC_VERSION_MINOR
# TCMALLOC_VERSION_PATCH

find_path(GPERFTOOLS_INCLUDE NAMES gperftools/profiler.h)
find_path(GPERFTOOLS_INCLUDE_DIR gperftools/profiler.h
HINTS $ENV{GPERF_ROOT}/include)
find_path(Tcmalloc_INCLUDE_DIR gperftools/tcmalloc.h
HINTS $ENV{GPERF_ROOT}/include)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Gperftools DEFAULT_MSG GPERFTOOLS_PROFILER GPERFTOOLS_INCLUDE)
if(Tcmalloc_INCLUDE_DIR AND EXISTS "${Tcmalloc_INCLUDE_DIR}/gperftools/tcmalloc.h")
foreach(ver "MAJOR" "MINOR" "PATCH")
file(STRINGS "${Tcmalloc_INCLUDE_DIR}/gperftools/tcmalloc.h" TC_VER_${ver}_LINE
REGEX "^#define[ \t]+TC_VERSION_${ver}[ \t]+[^ \t]+$")
string(REGEX REPLACE "^#define[ \t]+TC_VERSION_${ver}[ \t]+(\".)?([0-9]*)\"?$"
"\\2" TCMALLOC_VERSION_${ver} "${TC_VER_${ver}_LINE}")
unset(TC_VER_${ver}_LINE)
endforeach()
set(TCMALLOC_VERSION_STRING "${TCMALLOC_VERSION_MAJOR}.${TCMALLOC_VERSION_MINOR}")
if(NOT TCMALLOC_VERSION_PATCH STREQUAL "")
set(TCMALLOC_VERSION_STRING "${TCMALLOC_VERSION_STRING}.${TCMALLOC_VERSION_PATCH}")
endif()
endif()

foreach(component tcmalloc tcmalloc_minimal profiler)
string(TOUPPER ${component} COMPONENT)
find_library(GPERFTOOLS_${COMPONENT}_LIBRARY ${component}
HINTS $ENV{GPERF_ROOT}/lib)
list(APPEND GPERFTOOLS_LIBRARIES GPERFTOOLS_${COMPONENT}_LIBRARY)
endforeach()

set(GPERFTOOLS_INCLUDE_DIRS ${GMPXX_INCLUDE})
set(GPERFTOOLS_LIBRARIES ${GPERFTOOLS_PROFILER})
set(_Gperftools_FIND_REQUIRED_VARS "GPERFTOOLS_INCLUDE_DIR")
if(Gperftools_FIND_COMPONENTS)
foreach(component ${Gperftools_FIND_COMPONENTS})
string(TOUPPER ${component} COMPONENT)
list(APPEND _Gperftools_FIND_REQUIRED_VARS "GPERFTOOLS_${COMPONENT}_LIBRARY")
endforeach()
else()
list(APPEND _Gperftools_FIND_REQUIRED_VARS "GPERFTOOLS_LIBRARIES")
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Gperftools
FOUND_VAR Gperftools_FOUND
REQUIRED_VARS ${_Gperftools_FIND_REQUIRED_VARS}
VERSION_VAR TCMALLOC_VERSION_STRING)

add_library(Gperftools::Gperftools INTERFACE IMPORTED)
set_property(TARGET Gperftools::Gperftools PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${GPERFTOOLS_INCLUDE_DIRS}")
set_property(TARGET Gperftools::Gperftools PROPERTY INTERFACE_LINK_LIBRARIES "${GPERFTOOLS_LIBRARIES}")
mark_as_advanced(${GPERFTOOLS_LIBRARIES} GPERFTOOLS_INCLUDE_DIR)

mark_as_advanced(GPERFTOOLS_PROFILER GPERFTOOLS_INCLUDE GPERFTOOLS_INCLUDE_DIRS GPERFTOOLS_LIBRARIES)
if(Gperftools_FOUND)
foreach(Component Tcmalloc Tcmalloc_minimal Profiler)
if(NOT (TARGET Gperftools::${Component}))
string(TOUPPER ${Component} COMPONENT)
add_library(Gperftools::${Component} UNKNOWN IMPORTED)
set_target_properties(Gperftools::${Component} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${GPERFTOOLS_INCLUDE_DIR}"
IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
IMPORTED_LOCATION "${GPERFTOOLS_${COMPONENT}_LIBRARY}")
endif()
endforeach()
foreach(Component Tcmalloc Tcmalloc_minimal)
if(NOT (TARGET Gperftools::${Component}))
set_target_properties(Gperftools::${Component} PROPERTIES
INTERFACE_COMPILE_OPTIONS "-fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free")
endif()
endforeach()
endif()
74 changes: 59 additions & 15 deletions cmake/python-site.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,61 @@
try:
from setuptools.sysconfig import get_python_lib, get_config_vars
except ImportError:
from distutils.sysconfig import get_python_lib, get_config_vars
import sys
from argparse import ArgumentParser

if sys.argv[1] == "prefix":
print(get_python_lib(True, False, sys.argv[2] if len(sys.argv) > 2 else None))
elif sys.argv[1] == "suffix":
SO, SOABI, EXT_SUFFIX = get_config_vars("SO", "SOABI", "EXT_SUFFIX")
if EXT_SUFFIX is not None:
ext = EXT_SUFFIX
elif SOABI is not None:
ext = ''.join('.', SOABI, SO)
else:
ext = SO
print(ext)
if sys.version_info >= (3, 11):
NEW_STYLE = True
from sysconfig import get_config_var, get_config_vars, get_preferred_scheme, get_path
else:
NEW_STYLE = False
from site import USER_SITE
try:
from setuptools.sysconfig import get_python_lib, get_config_vars
except ImportError:
from distutils.sysconfig import get_python_lib, get_config_vars

parser = ArgumentParser()
if sys.version_info >= (3, 7):
subparser = parser.add_subparsers(required=True, dest="action")
else:
subparser = parser.add_subparsers(dest="action")

prefix_parser = subparser.add_parser("target")
prefix_group = prefix_parser.add_mutually_exclusive_group()
prefix_group.add_argument("--user", action='store_true', help='get user prefix')
prefix_group.add_argument("--prefix", type=str, help='prepend prefix')

prefix_parser = subparser.add_parser("suffix")

result = parser.parse_args()

if NEW_STYLE:
if result.action == "target":
if result.user:
scheme = get_preferred_scheme("user")
else:
scheme = get_preferred_scheme("prefix")
if result.prefix is not None:
cvars = get_config_vars()
cvars["base"] = result.prefix
cvars["platbase"] = result.prefix
platlib = get_path("platlib", scheme, cvars)
else:
platlib = get_path("platlib", scheme)
print(platlib)
elif result.action == "suffix":
print(get_config_var('EXT_SUFFIX'))
else:
# TODO: remove once python 3.10 is eol
if result.action == "target":
if result.user:
print(USER_SITE)
else:
print(get_python_lib(True, False, result.prefix))
elif result.action == "suffix":
SO, SOABI, EXT_SUFFIX = get_config_vars("SO", "SOABI", "EXT_SUFFIX")
if EXT_SUFFIX is not None:
ext = EXT_SUFFIX
elif SOABI is not None:
ext = ''.join('.', SOABI, SO)
else:
ext = SO
print(ext)
2 changes: 1 addition & 1 deletion libclingo-lpx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ endif()

if (CLINGOLPX_PROFILE)
target_compile_definitions(libclingo-lpx ${clingolpx_public_scope_} "CLINGOLPX_PROFILE")
target_link_libraries(libclingo-lpx ${clingolpx_public_scope_} Gperftools::Gperftools)
target_link_libraries(libclingo-lpx ${clingolpx_public_scope_} Gperftools::Profiler)
endif()
if (CLINGOLPX_CROSSCHECK)
target_compile_definitions(libclingo-lpx ${clingolpx_public_scope_} "CLINGOLPX_CROSSCHECK")
Expand Down
6 changes: 3 additions & 3 deletions libpyclingo-lpx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ set(source

if (NOT PYCLINGOLPX_INSTALL_DIR AND Python_EXECUTABLE)
if (PYCLINGOLPX_INSTALL STREQUAL "user")
execute_process(COMMAND ${Python_EXECUTABLE} ${CLINGOLPX_SOURCE_DIR}/cmake/python-site.py site OUTPUT_VARIABLE PYCLINGOLPX_INSTALL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${Python_EXECUTABLE} ${CLINGOLPX_SOURCE_DIR}/cmake/python-site.py target --user OUTPUT_VARIABLE PYCLINGOLPX_INSTALL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
elseif(PYCLINGOLPX_INSTALL STREQUAL "prefix")
execute_process(COMMAND ${Python_EXECUTABLE} ${CLINGOLPX_SOURCE_DIR}/cmake/python-site.py prefix "${CMAKE_INSTALL_PREFIX}" OUTPUT_VARIABLE PYCLINGOLPX_INSTALL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${Python_EXECUTABLE} ${CLINGOLPX_SOURCE_DIR}/cmake/python-site.py target --prefix "${CMAKE_INSTALL_PREFIX}" OUTPUT_VARIABLE PYCLINGOLPX_INSTALL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
else()
execute_process(COMMAND ${Python_EXECUTABLE} ${CLINGOLPX_SOURCE_DIR}/cmake/python-site.py prefix OUTPUT_VARIABLE PYCLINGOLPX_INSTALL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${Python_EXECUTABLE} ${CLINGOLPX_SOURCE_DIR}/cmake/python-site.py target OUTPUT_VARIABLE PYCLINGOLPX_INSTALL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()
endif()

Expand Down

0 comments on commit 7c61308

Please sign in to comment.