From 12cc39d0a11b1df366c8fc11732cbbe6f496eb6a Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 27 Oct 2023 13:51:13 +0100 Subject: [PATCH] Add support for building + testing both C++ & C# with CMake. This commit adds the ability to build both codebases with CMake. This is of course primarily aimed at portability for building with C++, but is nonetheless quite convenient for working on both of them at the same time. C# compilation is disabled by default since it requires you to be using the Visual Studio generator in order to work - however, you should be able to invoke the resulting project files from MSBuild if you don't have or want the full Visual Studio IDE. The code of the applications has been altered to separate unit tests out. They now do not run unless there is some explicit call to invoke them. This ensures that when CTest runs, it is able to execute the different kinds of test separately, preventing mixing of results in the event of a failure. --- CMakeLists.txt | 125 +++++++++++++++++++++ MiniScript-cpp/Makefile | 25 ----- MiniScript-cpp/README.md | 30 ++--- MiniScript-cpp/src/MiniScript/UnitTest.cpp | 6 + MiniScript-cpp/src/main.cpp | 3 - MiniScript-cs/Program.cs | 45 ++++---- 6 files changed, 167 insertions(+), 67 deletions(-) create mode 100755 CMakeLists.txt delete mode 100644 MiniScript-cpp/Makefile diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100755 index 0000000..8a6cc17 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,125 @@ +cmake_minimum_required(VERSION 3.12) +project(Miniscript VERSION 1.6.2 LANGUAGES C CXX) + +option(MINISCRIPT_BUILD_TESTING "Build unit test executable" OFF) +option(MINISCRIPT_BUILD_CSHARP "Build CSharp binaries" OFF) + +if(MINISCRIPT_BUILD_CSHARP) + enable_language(CSharp) + set(CMAKE_CSharp_FLAGS "/langversion:7.3") + set(CMAKE_DOTNET_TARGET_FRAMEWORK "net45") + set(CMAKE_DOTNET_TARGET_FRAMEWORK_VERSION "v4.5") + set(DOTNET_REFS "System;System.Core") + add_library(miniscript-cs SHARED + MiniScript-cs/Miniscript.cs + MiniScript-cs/MiniscriptErrors.cs + MiniScript-cs/MiniscriptInterpreter.cs + MiniScript-cs/MiniscriptIntrinsics.cs + MiniScript-cs/MiniscriptKeywords.cs + MiniScript-cs/MiniscriptLexer.cs + MiniScript-cs/MiniscriptParser.cs + MiniScript-cs/MiniscriptTAC.cs + MiniScript-cs/MiniscriptTypes.cs + MiniScript-cs/MiniscriptUnitTest.cs + ) + set_target_properties(miniscript-cs PROPERTIES + VS_GLOBAL_ROOTNAMESPACE "Miniscript" + VS_DOTNET_REFERENCES "${DOTNET_REFS}" + ) +endif() + +set(MINISCRIPT_HEADERS + MiniScript-cpp/src/MiniScript/Dictionary.h + MiniScript-cpp/src/MiniScript/List.h + MiniScript-cpp/src/MiniScript/MiniscriptErrors.h + MiniScript-cpp/src/MiniScript/MiniscriptInterpreter.h + MiniScript-cpp/src/MiniScript/MiniscriptIntrinsics.h + MiniScript-cpp/src/MiniScript/MiniscriptKeywords.h + MiniScript-cpp/src/MiniScript/MiniscriptLexer.h + MiniScript-cpp/src/MiniScript/MiniscriptParser.h + MiniScript-cpp/src/MiniScript/MiniscriptTAC.h + MiniScript-cpp/src/MiniScript/MiniscriptTypes.h + MiniScript-cpp/src/MiniScript/QA.h + MiniScript-cpp/src/MiniScript/RefCountedStorage.h + MiniScript-cpp/src/MiniScript/SimpleString.h + MiniScript-cpp/src/MiniScript/SimpleVector.h + MiniScript-cpp/src/MiniScript/SplitJoin.h + MiniScript-cpp/src/MiniScript/UnicodeUtil.h + MiniScript-cpp/src/MiniScript/UnitTest.h +) + +set(MINICMD_HEADERS + MiniScript-cpp/src/DateTimeUtils.h + MiniScript-cpp/src/OstreamSupport.h + MiniScript-cpp/src/ShellIntrinsics.h + MiniScript-cpp/src/editline/config.h + MiniScript-cpp/src/editline/editline.h + MiniScript-cpp/src/editline/editline_internal.h + MiniScript-cpp/src/editline/os9.h + MiniScript-cpp/src/editline/unix.h + MiniScript-cpp/src/whereami/whereami.h +) + +add_library(miniscript-cpp + MiniScript-cpp/src/MiniScript/Dictionary.cpp + MiniScript-cpp/src/MiniScript/List.cpp + MiniScript-cpp/src/MiniScript/MiniscriptInterpreter.cpp + MiniScript-cpp/src/MiniScript/MiniscriptIntrinsics.cpp + MiniScript-cpp/src/MiniScript/MiniscriptKeywords.cpp + MiniScript-cpp/src/MiniScript/MiniscriptLexer.cpp + MiniScript-cpp/src/MiniScript/MiniscriptParser.cpp + MiniScript-cpp/src/MiniScript/MiniscriptTAC.cpp + MiniScript-cpp/src/MiniScript/MiniscriptTypes.cpp + MiniScript-cpp/src/MiniScript/QA.cpp + MiniScript-cpp/src/MiniScript/SimpleString.cpp + MiniScript-cpp/src/MiniScript/SimpleVector.cpp + MiniScript-cpp/src/MiniScript/SplitJoin.cpp + MiniScript-cpp/src/MiniScript/UnicodeUtil.cpp + MiniScript-cpp/src/MiniScript/UnitTest.cpp + ${MINISCRIPT_HEADERS} +) + +target_include_directories(miniscript-cpp PUBLIC src/MiniScript) + +if(NOT WIN32) + set(EDITLINE_SRC + MiniScript-cpp/src/editline/complete.c + MiniScript-cpp/src/editline/editline.c + MiniScript-cpp/src/editline/sysos9.c + MiniScript-cpp/src/editline/sysunix.c + ) +endif() + +add_executable(minicmd + MiniScript-cpp/src/main.cpp + MiniScript-cpp/src/DateTimeUtils.cpp + MiniScript-cpp/src/OstreamSupport.cpp + MiniScript-cpp/src/ShellIntrinsics.cpp + MiniScript-cpp/src/whereami/whereami.c + ${EDITLINE_SRC} + ${MINICMD_HEADERS} +) +target_include_directories(minicmd PRIVATE src/editline) +target_link_libraries(minicmd PRIVATE miniscript-cpp) + +if(MINISCRIPT_BUILD_TESTING) + enable_testing() + add_custom_target(TestSuite SOURCES TestSuite.txt) + add_executable(tests-cpp MiniScript-cpp/src/MiniScript/UnitTest.cpp) + target_compile_definitions(tests-cpp PRIVATE UNIT_TEST_MAIN) + target_link_libraries(tests-cpp PRIVATE miniscript-cpp) + add_test(NAME Miniscript.cpp.UnitTests COMMAND tests-cpp) + add_test(NAME Miniscript.cpp.Integration COMMAND minicmd --itest ${CMAKE_SOURCE_DIR}/TestSuite.txt) + set_tests_properties(Miniscript.cpp.UnitTests Miniscript.cpp.Integration PROPERTIES FAIL_REGULAR_EXPRESSION "FAIL|Error") + if(MINISCRIPT_BUILD_CSHARP) + add_executable(tests-cs MiniScript-cs/Program.cs) + target_link_libraries(tests-cs PRIVATE miniscript-cs) + set_target_properties(tests-cs PROPERTIES + VS_GLOBAL_ROOTNAMESPACE "Miniscript" + VS_DOTNET_REFERENCES "${DOTNET_REFS}" + ) + add_test(NAME Miniscript.cs.UnitTests COMMAND tests-cs --test) + add_test(NAME Miniscript.cs.Integration COMMAND tests-cs --test --integration ${CMAKE_SOURCE_DIR}/TestSuite.txt) + set_tests_properties(Miniscript.cs.UnitTests Miniscript.cs.Integration PROPERTIES FAIL_REGULAR_EXPRESSION "FAIL|Error") + endif() +endif() diff --git a/MiniScript-cpp/Makefile b/MiniScript-cpp/Makefile deleted file mode 100644 index 0998d0a..0000000 --- a/MiniScript-cpp/Makefile +++ /dev/null @@ -1,25 +0,0 @@ -CC=gcc -CFLAGS=-c -Isrc -Isrc/editline -Isrc/whereami -Isrc/MiniScript -fsigned-char -CPPFLAGS=$(CFLAGS) -std=c++11 -LIBS=-lstdc++ -lm - -miniscript: src/*.h src/*.cpp src/MiniScript/*.h src/MiniScript/*.cpp src/editline/*.h src/editline/*.c src/whereami/*.c src/whereami/*.h - $(CC) $(CFLAGS) src/editline/complete.c src/editline/editline.c src/editline/sysunix.c src/whereami/whereami.c - $(CC) $(CPPFLAGS) src/*.cpp src/MiniScript/*.cpp - $(CC) -o miniscript *.o $(LIBS) - rm -f *.o - -.PHONY: clean - -install: - chmod ugo+x miniscript - rm -f /usr/local/bin/miniscript - ln -s ${shell pwd}/miniscript /usr/local/bin/ - -clean: - rm -f *.o - rm -f miniscript - -uninstall: - rm -f *.o - rm -f miniscript /usr/local/bin/miniscript diff --git a/MiniScript-cpp/README.md b/MiniScript-cpp/README.md index 1dd9feb..8239848 100644 --- a/MiniScript-cpp/README.md +++ b/MiniScript-cpp/README.md @@ -2,35 +2,25 @@ This folder contains the source code of the C++ implementation of the [MiniScript scripting language](http://miniscript.org), including the command-line host program. -## Building for macOS +## Building -Open the Xcode project (MiniScript.xcodeproj). Build. +MiniScript is built with CMake. You can generate your desired flavour of build files as usual from either the CMake GUI or using `cmake` on the commandline. If you are unfamiliar with CMake and want to build right now, use the GUI. If you cannot use the GUI, make a directory somewhere and, while in that directory, run `cmake path/to/miniscript` followed by `cmake --build` -(Note that if you run within XCode, it will work, but every input keystroke in the console will be doubled, because the readline library doesn't quite work properly with Xcode's console. It works fine in a real Terminal window though.) +Miniscript itself will be output as a shared library for you to link to. You can even `include()` the CMakeLists.txt of this project inside your own for clean dependency management. -You can also follow the Linux procedure, if you prefer command-line tools. +If you are only interested in the C# edition of MiniScript, there is a project file provided in the respective directory. -## Building for Linux +### CMake Build Options -Prerequisites: You will need make, gcc, and g++ installed. +Options can be controlled in the usual way - either within the CMake GUI or by passing `-D