Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ GTAGS
.projectile
.lean_options
.vs
src/emacs/dependencies
/src/stage1
compile_commands.json
*.idea
tasks.json
Expand All @@ -24,3 +24,5 @@ settings.json
CMakeSettings.json
CppProperties.json
/library/Makefile
/bin/*.a
/bin/leanc
19 changes: 19 additions & 0 deletions bin/leanc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
# Lean compiler
#
# A simple wrapper around a C++ compiler. Defaults to the compiler Lean was built with,
# which can be overridden with the environment variable `LEAN_CXX`. All parameters are passed
# as-is to the wrapped compiler.
#
# Interesting options:
# * `-U LEAN_MULTI_THREAD` can be used to optimize programs not making use of multi-threading

set -e
bindir=$(dirname $0)
for cxx in $LEAN_CXX @CMAKE_CXX_COMPILER@ /usr/bin/g++; do
if [ -f $cxx ] || command -v $cxx; then
export LEAN_CXX=$cxx && break
fi
done
[ -f $LEAN_CXX ] || command -v $LEAN_CXX || error "no suitable C++ compiler found!"
$LEAN_CXX -std=c++14 -D LEAN_MULTI_THREAD -pthread -fPIC "-I$bindir/../src" "$@" "-L$bindir" -ldl -Wl,-Bstatic -lleanstatic -lleanstdlib -lgmp @LEANC_EXTRA_LIBS@ -static-libgcc -static-libstdc++ -Wl,-Bdynamic -Wno-unused-command-line-argument
22 changes: 14 additions & 8 deletions library/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ SRCS = $(shell find . -name '*.lean')
OBJS = $(SRCS:.lean=.olean)
DEPS = $(SRCS:.lean=.depend)
OPTS = @LEAN_EXTRA_MAKE_OPTS@
BOOT_DIR = ../src/boot
CPPS = $(addprefix $(BOOT_DIR)/,$(patsubst %.lean,%.cpp,$(SRCS)))
STAGE0_DIR = ../src/boot
STAGE1_DIR = ../src/stage1
CPPS = $(addprefix $(STAGE1_DIR)/,$(patsubst %.lean,%.cpp,$(SRCS)))
# ensure deterministic ordering
CPPS_CORE=$(sort $(patsubst %.lean,%.cpp,$(SRCS)))

Expand All @@ -29,16 +30,21 @@ clean:
find . -name *.olean -delete
find . -name *.depend -delete

$(BOOT_DIR)/%.cpp: %.lean %.olean
$(STAGE1_DIR)/%.cpp: %.lean %.olean
@mkdir -p $(@D)
$(LEAN) $(OPTS) --cpp=$@ $<

updatecpp: $(CPPS)
echo "add_library (boot OBJECT $(CPPS_CORE))" > $(BOOT_DIR)/CMakeLists.txt
$(STAGE1_DIR)/%.o: $(STAGE1_DIR)/%.cpp
../bin/leanc -c -o $@ $< -O3

cleancpp:
find $(BOOT_DIR) -name '*.cpp' -delete
$(STAGE1_DIR)/libleanstdlib.a: $(patsubst %.cpp,%.o,$(CPPS))
@ar rcs $@ $^

.PRECIOUS: %.depend
update-stage0:
rm -r $(STAGE0_DIR)/init
cp -R $(STAGE1_DIR)/init $(STAGE0_DIR)/init
echo "add_library (boot OBJECT $(CPPS_CORE))" > $(STAGE0_DIR)/CMakeLists.txt

.PRECIOUS: %.depend $(STAGE1_DIR)/%.cpp

include $(DEPS)
53 changes: 42 additions & 11 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ option(SMALL_ALLOCATOR "SMALL_ALLOCATOR" ON)
option(LAZY_RC "LAZY_RC" OFF)
option(RUNTIME_STATS "RUNTIME_STATS" OFF)

# development-specific options
option(CHECK_OLEAN_VERSION "Only load .olean files compiled with the current version of Lean" ON)
option(REBUILD_STAGE0 "Automatically rebuild lean_stage0 when out of date. Can be temporarily be disabled while re-bootstrapping." ON)

# library dir
set(LIBRARY_DIR "lib/lean" CACHE STRING "library dir")
Expand Down Expand Up @@ -321,7 +323,7 @@ else()
# GMP
find_package(GMP 5.0.5 REQUIRED)
include_directories(${GMP_INCLUDE_DIR})
set(EXTRA_LIBS ${EXTRA_LIBS} ${GMP_LIBRARIES})
set(COPY_LIBS ${COPY_LIBS} ${GMP_LIBRARIES})
endif()

# DL
Expand All @@ -342,7 +344,8 @@ endif()
if(JEMALLOC)
find_package(Jemalloc)
if(${JEMALLOC_FOUND})
set(EXTRA_UTIL_LIBS ${EXTRA_UTIL_LIBS} ${JEMALLOC_LIBRARIES})
set(COPY_LIBS ${COPY_LIBS} ${JEMALLOC_LIBRARIES})
set(LEANC_EXTRA_LIBS "${LEANC_EXTRA_LIBS} -ljemalloc")
message(STATUS "Using jemalloc.")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D HAS_JEMALLOC")
else()
Expand All @@ -355,7 +358,8 @@ if(NOT "${JEMALLOC_FOUND}")
if(TCMALLOC)
find_package(Tcmalloc)
if(${TCMALLOC_FOUND})
set(EXTRA_LIBS ${EXTRA_LIBS} ${TCMALLOC_LIBRARIES})
set(COPY_LIBS ${COPY_LIBS} ${TCMALLOC_LIBRARIES})
set(LEANC_EXTRA_LIBS "${LEANC_EXTRA_LIBS} -ltcmalloc")
message(STATUS "Using tcmalloc.")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D HAS_TCMALLOC")
else()
Expand All @@ -366,7 +370,7 @@ if(NOT "${JEMALLOC_FOUND}")
endif()
endif()

set(EXTRA_LIBS ${EXTRA_LIBS} ${EXTRA_UTIL_LIBS})
set(EXTRA_LIBS ${EXTRA_LIBS} ${COPY_LIBS})

# Python
find_package(PythonInterp)
Expand Down Expand Up @@ -441,31 +445,53 @@ set(LEAN_OBJS ${LEAN_OBJS} $<TARGET_OBJECTS:lean_frontend>)
add_subdirectory(init)
set(LEAN_OBJS ${LEAN_OBJS} $<TARGET_OBJECTS:init>)
add_subdirectory(boot)
set(LEAN_OBJS ${LEAN_OBJS} $<TARGET_OBJECTS:boot>)
if(STAGE1)
add_subdirectory(stage1)
endif()
if(EMSCRIPTEN)
set(LEAN_LIBRARY_TYPE SHARED)
else()
set(LEAN_LIBRARY_TYPE STATIC)
endif()

add_library(leanstatic_stage0 ${LEAN_LIBRARY_TYPE} ${LEAN_OBJS} $<TARGET_OBJECTS:boot>)
target_link_libraries(leanstatic_stage0 ${EXTRA_LIBS})

add_custom_target(build_leanstdlib
# '-G Ninja' complains otherwise
BYPRODUCTS "${LEAN_SOURCE_DIR}/stage1/libleanstdlib.a"
WORKING_DIRECTORY "${LEAN_SOURCE_DIR}/../library"
COMMAND make -j8 ../src/stage1/libleanstdlib.a "LEAN=$<TARGET_FILE:lean_stage0>"
DEPENDS lean_stage0)

add_library(leanstatic ${LEAN_LIBRARY_TYPE} ${LEAN_OBJS})
target_link_libraries(leanstatic ${EXTRA_LIBS})
target_link_libraries(leanstatic "${LEAN_SOURCE_DIR}/stage1/libleanstdlib.a" ${EXTRA_LIBS})
add_dependencies(leanstatic build_leanstdlib)

ADD_CUSTOM_TARGET(bin_lib ALL
COMMAND "${CMAKE_COMMAND}" -E make_directory "${LEAN_SOURCE_DIR}/../bin"
COMMAND "${CMAKE_COMMAND}" -E copy "${CMAKE_CURRENT_BINARY_DIR}/libleanstatic*" "${LEAN_SOURCE_DIR}/../bin"
COMMAND "${CMAKE_COMMAND}" -E copy ${COPY_LIBS} "${LEAN_SOURCE_DIR}/stage1/libleanstdlib*" "${CMAKE_CURRENT_BINARY_DIR}/libleanstatic*" "${LEAN_SOURCE_DIR}/../bin"
DEPENDS leanstatic
)
# Configure leanc
configure_file("${LEAN_SOURCE_DIR}/../bin/leanc.in" "${CMAKE_CURRENT_BINARY_DIR}/tmp/leanc")
file(COPY "${CMAKE_CURRENT_BINARY_DIR}/tmp/leanc"
DESTINATION "${LEAN_SOURCE_DIR}/../bin"
FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
install(FILES "${LEAN_SOURCE_DIR}/../bin/leanc"
DESTINATION bin
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)

# The DLL (shared library) is not being generated correctly when we use cross-compilation (i.e., generate the Windows DLL using Linux).
# For some strange reason, it contains a copy of pthread_equal.
# Remark: this problem does not happen when we generate the DLL using msys2 on Windows.
if (NOT("${CROSS_COMPILE}" MATCHES "ON"))
if ("${STATIC}" MATCHES "OFF")
add_library(leanshared SHARED shared/init.cpp ${LEAN_OBJS})
target_link_libraries(leanshared ${EXTRA_LIBS})
add_library(leanshared SHARED shared/init.cpp)
target_link_libraries(leanshared leanstatic)
install(TARGETS leanshared DESTINATION lib)
endif()
install(TARGETS leanstatic DESTINATION lib)
install(FILES libleanstatic.a DESTINATION lib)
endif()

add_subdirectory(shell)
Expand Down Expand Up @@ -500,7 +526,7 @@ endfunction()
escape_regex(LEAN_SOURCE_DIR_REGEX_SAFE ${LEAN_SOURCE_DIR})

foreach(SOURCE ${LEAN_SOURCES})
if(${SOURCE} MATCHES "${LEAN_SOURCE_DIR_REGEX_SAFE}/boot/.*")
if(${SOURCE} MATCHES "${LEAN_SOURCE_DIR_REGEX_SAFE}/(boot|stage.)/.*")
LIST(REMOVE_ITEM LEAN_SOURCES ${SOURCE})
endif()
endforeach()
Expand All @@ -527,6 +553,11 @@ add_custom_target(clean-std-lib
COMMAND make clean
)

add_custom_target(clean-stage1
WORKING_DIRECTORY "${LEAN_SOURCE_DIR}"
COMMAND "${CMAKE_COMMAND}" -E remove_directory stage1
)

add_custom_target(clean-leanpkg
WORKING_DIRECTORY "${LEAN_SOURCE_DIR}/../leanpkg"
COMMAND "${CMAKE_COMMAND}" -P "${CMAKE_MODULE_PATH}/CleanOlean.cmake"
Expand Down
16 changes: 13 additions & 3 deletions src/shell/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
if(REBUILD_STAGE0)
add_executable(lean_stage0 lean.cpp)
target_link_libraries(lean_stage0 leanstatic_stage0)
else()
add_executable(lean_stage0 IMPORTED)
set_target_properties(lean_stage0 PROPERTIES IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/lean_stage0")
endif()

add_custom_target(update-stage0
COMMAND make update-stage0
DEPENDS lean
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/../library")

add_executable(lean lean.cpp)
target_link_libraries(lean leanstatic)
install(TARGETS lean DESTINATION bin)

# Configure compiler test scripts
configure_file("${LEAN_SOURCE_DIR}/../tests/compiler/test_flags.sh.in" "${LEAN_SOURCE_DIR}/../tests/compiler/test_flags.sh")

if(${EMSCRIPTEN})
set(NODE_JS "node --stack_size=8192")

Expand Down
9 changes: 4 additions & 5 deletions src/tests/kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
set(kernel_tst_objs ${LEAN_OBJS})
add_executable(level level.cpp ${kernel_tst_objs})
target_link_libraries(level ${EXTRA_LIBS})
add_executable(level level.cpp)
target_link_libraries(level leanstatic)
add_exec_test(level "level")
add_executable(max_sharing max_sharing.cpp ${kernel_tst_objs})
target_link_libraries(max_sharing ${EXTRA_LIBS})
add_executable(max_sharing max_sharing.cpp)
target_link_libraries(max_sharing leanstatic)
add_exec_test(max_sharing "max_sharing")
25 changes: 12 additions & 13 deletions src/tests/library/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
set(library_tst_objs ${LEAN_OBJS})
add_executable(expr_lt expr_lt.cpp ${library_tst_objs})
target_link_libraries(expr_lt ${EXTRA_LIBS})
add_executable(expr_lt expr_lt.cpp)
target_link_libraries(expr_lt leanstatic)
add_exec_test(expr_lt "expr_lt")
add_executable(deep_copy deep_copy.cpp ${library_tst_objs})
target_link_libraries(deep_copy ${EXTRA_LIBS})
add_executable(deep_copy deep_copy.cpp)
target_link_libraries(deep_copy leanstatic)
add_exec_test(deep_copy "deep_copy")
add_executable(occurs occurs.cpp ${library_tst_objs})
target_link_libraries(occurs ${EXTRA_LIBS})
add_executable(occurs occurs.cpp)
target_link_libraries(occurs leanstatic)
add_exec_test(occurs "occurs")
add_executable(head_map head_map.cpp ${library_tst_objs})
target_link_libraries(head_map ${EXTRA_LIBS})
add_executable(head_map head_map.cpp)
target_link_libraries(head_map leanstatic)
add_exec_test(head_map "head_map")
add_executable(parray parray.cpp ${library_tst_objs})
target_link_libraries(parray ${EXTRA_LIBS})
add_executable(parray parray.cpp)
target_link_libraries(parray leanstatic)
add_exec_test(parray "parray")
add_executable(phashtable phashtable.cpp ${library_tst_objs})
target_link_libraries(phashtable ${EXTRA_LIBS})
add_executable(phashtable phashtable.cpp)
target_link_libraries(phashtable leanstatic)
add_exec_test(phashtable "phashtable")
4 changes: 0 additions & 4 deletions tests/compiler/test_flags.sh.in

This file was deleted.

14 changes: 1 addition & 13 deletions tests/compiler/test_single.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ if [ $# -ne 3 -a $# -ne 2 ]; then
exit 1
fi
ulimit -s 8192
source test_flags.sh
LEAN=$1
BIN_DIR=../../bin
INCLUDE_DIR=../../src
export LEAN_PATH=../../library:.
if [ $# -ne 3 ]; then
INTERACTIVE=no
Expand All @@ -16,16 +14,6 @@ else
fi
ff=$2

# Hack for converting a list of libraries such as `/usr/lib/gmp.so;dl` into valid liker parameters.
LINKER_LIBS=""
for lib in ${LIBS//;/ }; do
if [[ $lib == *".so" || $lib == *".dll" || $lib == *".dylib" ]]; then
LINKER_LIBS="$LINKER_LIBS $lib"
else
LINKER_LIBS="$LINKER_LIBS -l$lib"
fi
done

if [[ "$OSTYPE" == "msys" ]]; then
# Windows running MSYS2
# Replace /c/ with c:, and / with \\
Expand All @@ -43,7 +31,7 @@ if [ $? -ne 0 ]; then
exit 1
fi

$CXX -o "$ff.out" -I $INCLUDE_DIR $CXX_FLAGS $ff.cpp $BIN_DIR/libleanstatic.a $LINKER_FLAGS $LINKER_LIBS
$BIN_DIR/leanc -o "$ff.out" $ff.cpp
if [ $? -ne 0 ]; then
echo "Failed to compile C++ file $ff.cpp"
exit 1
Expand Down
14 changes: 1 addition & 13 deletions tests/playground/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,11 @@ if [ $# -eq 0 ]; then
exit 1
fi
ulimit -s 8192
source ../compiler/test_flags.sh
BIN_DIR=../../bin
LEAN=$BIN_DIR/lean
INCLUDE_DIR=../../src
export LEAN_PATH=../../library:.
ff=$1

# Hack for converting a list of libraries such as `/usr/lib/gmp.so;dl` into valid liker parameters.
LINKER_LIBS=""
for lib in ${LIBS//;/ }; do
if [[ $lib == *".so" || $lib == *".dll" || $lib == *".dylib" ]]; then
LINKER_LIBS="$LINKER_LIBS $lib"
else
LINKER_LIBS="$LINKER_LIBS -l$lib"
fi
done

if [[ "$OSTYPE" == "msys" ]]; then
# Windows running MSYS2
# Replace /c/ with c:, and / with \\
Expand All @@ -33,7 +21,7 @@ if [ $? -ne 0 ]; then
exit 1
fi

$CXX -o "$ff.out" -I $INCLUDE_DIR $CXX_FLAGS $ff.cpp $BIN_DIR/libleanstatic.a $LINKER_FLAGS $LINKER_LIBS
$BIN_DIR/leanc -o "$ff.out" $ff.cpp
if [ $? -ne 0 ]; then
echo "Failed to compile C++ file $ff.cpp"
exit 1
Expand Down