commit 1022fb4e32840fdc4123b989d78f4323de847c9b
parent 0ec12c02be70302e911aad7dea046d0fcff21ccd
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 26 Apr 2024 14:13:03 +0200
Replace CMake by POSIX make
The build procedure is written in POSIX make, which the user can
configure via the config.mk file. The make.sh script contains commands
that could be found directly in POSIX make, but which are placed here to
simplify writing the Makefile.
In addition to the features previously provided by its CMake
alternative, this Makefile supports the use of static libraries and
provides an uninstall target. It also enable compiler and linker options
that activate various hardening features aimed at increasing the
security and robustness of generated binaries. In any case, the main
motivation behind its writing is to use a good old well-established
standard with simple features, available on all UNIX systems, thus
simplifying its portability and support while being much lighter
Please note that the generation of man pages is not managed by the
Makefile. Current manual pages will be translated into mandoc macros to
avoid using the cumbersome asciidoc tool suite, and to ensure
consistency with other manual pages by construction. As a result, no
intermediate manual page generation step will be necessary.
Diffstat:
| M | .gitignore | | | 12 | ++++++------ |
| A | Makefile | | | 106 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| D | cmake/CMakeLists.txt | | | 142 | ------------------------------------------------------------------------------- |
| D | cmake/doc/CMakeLists.txt | | | 149 | ------------------------------------------------------------------------------- |
| A | config.mk | | | 79 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | make.sh | | | 39 | +++++++++++++++++++++++++++++++++++++++ |
6 files changed, 230 insertions(+), 297 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,12 +1,12 @@
.gitignore
-CMakeCache.txt
-CMakeFiles
-Makefile
-tmp
[Bb]uild*
*.sw[po]
-*.[ao]
-*.orig
+*.[aod]
+*.so
*~
+.config
+src/green-default.h
+src/green-version.h
+sgreen
tags
diff --git a/Makefile b/Makefile
@@ -0,0 +1,106 @@
+# Copyright (C) 2020-2022, 2024 |Méso|Star> (contact@meso-star.com)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+.POSIX:
+.SUFFIXES: # Clean up default inference rules
+
+include config.mk
+
+default: build_executable
+
+################################################################################
+# Program building
+################################################################################
+SRC =\
+ src/green-args.c\
+ src/green-compute.c\
+ src/green-input.c\
+ src/green-main.c\
+ src/green-output.c
+OBJ = $(SRC:.c=.o)
+DEP = $(SRC:.c=.d)
+
+# Headers to configure
+HDR =\
+ src/green-default.h\
+ src/green-version.h
+
+build_executable: .config $(HDR) $(DEP)
+ @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) sgreen
+
+$(DEP) $(HDR) $(OBJ): config.mk
+
+sgreen: $(OBJ)
+ $(CC) $(CFLAGS) $(DPDC_CFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(DPDC_LIBS)
+
+.config: config.mk
+ @if ! $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys; then \
+ echo "rsys $(RSYS_VERSION) not found" >&2; exit 1; fi
+ @if ! $(PKG_CONFIG) --atleast-version $(STARDIS_VERSION) stardis; then \
+ echo "stardis $(STARDIS_VERSION) not found" >&2; exit 1; fi
+ @echo "config done" > $@
+
+src/green-default.h: src/green-default.h.in
+ sed -e 's#@GREEN_ARGS_DEFAULT_VERBOSE_LEVEL@#$(GREEN_ARGS_DEFAULT_VERBOSE_LEVEL)#g'\
+ $@.in > $@
+
+src/green-version.h: src/green-version.h.in
+ sed -e 's/@GREEN_VERSION_MAJOR@/$(VERSION_MAJOR)/' \
+ -e 's/@GREEN_VERSION_MINOR@/$(VERSION_MINOR)/' \
+ -e 's/@GREEN_VERSION_PATCH@/$(VERSION_PATCH)/' \
+ $@.in > $@
+
+.SUFFIXES: .c .d .o
+.c.d:
+ @$(CC) $(CFLAGS) $(DPDC_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@
+
+.c.o:
+ $(CC) $(CFLAGS) $(DPDC_CFLAGS) -c $< -o $@
+
+################################################################################
+# Installation
+################################################################################
+install: build_executable
+ @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/bin" sgreen
+ #@$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/man/man1" doc/sgreen.1
+ #@$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/man/man5" doc/sgreen-input.5
+ #@$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/man/man5" doc/sgreen-output.5
+ @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/stardis" COPYING
+ @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/stardis" README.md
+
+uninstall:
+ rm -f "$(DESTDIR)$(PREFIX)/bin/sgreen"
+ rm -f "$(DESTDIR)$(PREFIX)/share/man/man1/sgreen.1"
+ rm -f "$(DESTDIR)$(PREFIX)/share/man/man5/sgreen-input.5"
+ rm -f "$(DESTDIR)$(PREFIX)/share/man/man5/sgreen-output.5"
+ rm -f "$(DESTDIR)$(PREFIX)/share/doc/stardis/COPYING"
+ rm -f "$(DESTDIR)$(PREFIX)/share/doc/stardis/README.md"
+
+################################################################################
+# Miscellaneous targets
+################################################################################
+all: build_library build_tests
+
+clean:
+ rm -f $(OBJ) $(HDR) .config sgreen
+
+distclean: clean
+ rm -f $(DEP)
+
+lint:
+ shellcheck -o all make.sh
+ #mandoc -Tlint -Wall doc/stardis.1 || [ $$? -le 1 ]
+ #mandoc -Tlint -Wall doc/stardis-input.5 || [ $$? -le 1 ]
+ #mandoc -Tlint -Wall doc/stardis-output.5 || [ $$? -le 1 ]
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -1,142 +0,0 @@
-# Copyright (C) 2020-2022 |Meso|Star>
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-cmake_minimum_required(VERSION 3.0)
-project(sgreen C)
-
-set(GREEN_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src)
-
-if(CMAKE_HOST_UNIX)
- set(GREEN_DOC "TROFF" CACHE STRING
- "Type of documentation to generate and install.")
-else()
- set(GREEN_DOC "HTML" CACHE STRING
- "Type of documentation to generate and install.")
-endif()
-
-set_property(CACHE GREEN_DOC PROPERTY STRINGS
- "HTML"
- "TROFF"
- "TROFF & HTML"
- "NONE")
-
-###############################################################################
-# Generate files
-###############################################################################
-set(GREEN_ARGS_DEFAULT_VERBOSE_LEVEL "1")
-
-configure_file(${GREEN_SOURCE_DIR}/../doc/sgreen.1.txt.in
- ${CMAKE_CURRENT_BINARY_DIR}/doc/sgreen.1.txt @ONLY)
-
-set(GREEN_VERSION_MAJOR 0)
-set(GREEN_VERSION_MINOR 4)
-set(GREEN_VERSION_PATCH 0)
-set(GREEN_VERSION ${GREEN_VERSION_MAJOR}.${GREEN_VERSION_MINOR}.${GREEN_VERSION_PATCH})
-
-configure_file(${GREEN_SOURCE_DIR}/green-default.h.in
- ${CMAKE_CURRENT_BINARY_DIR}/green-default.h @ONLY)
-
-configure_file(${GREEN_SOURCE_DIR}/green-version.h.in
- ${CMAKE_CURRENT_BINARY_DIR}/green-version.h @ONLY)
-
-###############################################################################
-# Check dependencies
-###############################################################################
-find_package(RCMake 0.4 REQUIRED)
-find_package(RSys 0.12 REQUIRED)
-find_package(stardis-green-types 4 REQUIRED)
-find_package(OpenMP 2.0 REQUIRED)
-if(MSVC)
- find_package(MuslGetopt REQUIRED)
-endif()
-
-include_directories(
- ${RSys_INCLUDE_DIR}
- ${CMAKE_CURRENT_BINARY_DIR})
-if(MSVC)
- include_directories(${MuslGetopt_INCLUDE_DIR})
-endif()
-
-set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR})
-include(rcmake)
-include(rcmake_runtime)
-
-if(CMAKE_COMPILER_IS_GNUCC)
- rcmake_append_runtime_dirs(_runtime_dirs RSys)
-endif()
-if(MSVC)
- rcmake_append_runtime_dirs(_runtime_dirs RSys MuslGetopt)
-endif()
-
-###############################################################################
-# Build subprojects
-###############################################################################
-if(NOT GREEN_DOC STREQUAL "NONE")
- add_subdirectory(doc)
-endif()
-
-###############################################################################
-# Configure and define targets
-###############################################################################
-set(GREEN_FILES_SRC
- green-args.c
- green-compute.c
- green-input.c
- green-main.c
- green-output.c)
-
-set(GREEN_FILES_INC
- green-args.h
- green-compute.h
- green-output.h
- green-types.h
- green-version.h.in)
-
-set(GREEN_FILES_DOC COPYING README.md)
-
-# Prepend each file by `GREEN_SOURCE_DIR'
-rcmake_prepend_path(GREEN_FILES_SRC ${GREEN_SOURCE_DIR})
-rcmake_prepend_path(GREEN_FILES_INC ${GREEN_SOURCE_DIR})
-rcmake_prepend_path(GREEN_FILES_DOC ${PROJECT_SOURCE_DIR}/../)
-
-add_executable(sgreen
- ${GREEN_FILES_SRC}
- ${GREEN_FILES_INC})
-
-set_target_properties(sgreen PROPERTIES
- COMPILE_FLAGS "${OpenMP_C_FLAGS}"
- VERSION ${GREEN_VERSION})
-
-if(CMAKE_COMPILER_IS_GNUCC)
- set(MATH_LIB m)
- set_target_properties(sgreen PROPERTIES LINK_FLAGS "${OpenMP_C_FLAGS}")
-elseif(MSVC)
- set(GETOPT_LIB MuslGetopt)
-endif()
-
-target_link_libraries(sgreen
- RSys ${GETOPT_LIB} ${MATH_LIB})
-
-###############################################################################
-# Define output & install directories
-###############################################################################
-install(TARGETS sgreen
- ARCHIVE DESTINATION bin
- LIBRARY DESTINATION lib
- RUNTIME DESTINATION bin)
-
-install(FILES ${GREEN_FILES_DOC} DESTINATION share/doc/sgreen)
-
-rcmake_copy_runtime_libraries(sgreen)
diff --git a/cmake/doc/CMakeLists.txt b/cmake/doc/CMakeLists.txt
@@ -1,149 +0,0 @@
-# Copyright (C) 2020-2022 |Meso|Star>
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-cmake_minimum_required(VERSION 3.0)
-
-string(REGEX MATCH ".*HTML.*" _html ${GREEN_DOC})
-string(REGEX MATCH ".*ROFF.*" _roff ${GREEN_DOC})
-
-set(GREEN_DOC_DIR ${PROJECT_SOURCE_DIR}/../doc)
-
-################################################################################
-# Look for asciidoc and a2x programs
-################################################################################
-if(_html)
- find_program(ASCIIDOC NAMES asciidoc asciidoc.py)
- if(NOT ASCIIDOC)
- unset(_html)
- message(WARNING
- "The `asciidoc' program is missing. "
- "The sgreen HTML documentation cannot be generated.")
- endif()
-endif()
-
-if(_roff)
- find_program(A2X NAMES a2x a2x.py)
- if(NOT A2X)
- unset(_roff)
- message(WARNING
- "The `a2x' program is missing. "
- "The sgreen man pages cannot be generated.")
- endif()
-endif()
-
-################################################################################
-# Copy doc files
-################################################################################
-set(MAN_NAMES
- sgreen-input.5
- sgreen-output.5)
-
-if(_roff OR _html)
- set(MAN_FILES)
- foreach(_name IN LISTS MAN_NAMES)
- set(_src ${GREEN_DOC_DIR}/${_name}.txt)
- set(_dst ${CMAKE_CURRENT_BINARY_DIR}/${_name}.txt)
- add_custom_command(
- OUTPUT ${_dst}
- COMMAND ${CMAKE_COMMAND} -E copy ${_src} ${_dst}
- DEPENDS ${_src}
- COMMENT "Copy the asciidoc ${_src}"
- VERBATIM)
- list(APPEND MAN_FILES ${_dst})
- endforeach()
- add_custom_target(man-copy ALL DEPENDS ${MAN_FILES})
-endif()
-
-list(APPEND MAN_NAMES sgreen.1)
-
-################################################################################
-# ROFF man pages
-################################################################################
-if(_roff)
- set(A2X_OPTS -dmanpage -fmanpage)
- set(MAN_FILES)
- set(MAN5_FILES)
- set(MAN1_FILES)
- foreach(_name IN LISTS MAN_NAMES)
- set(_man ${CMAKE_CURRENT_BINARY_DIR}/${_name})
- set(_txt ${CMAKE_CURRENT_BINARY_DIR}/${_name}.txt)
-
- add_custom_command(
- OUTPUT ${_man}
- COMMAND ${A2X} ${A2X_OPTS} ${_txt}
- DEPENDS man-copy ${_txt}
- WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
- COMMENT "Build ROFF man page ${_man}"
- VERBATIM)
- list(APPEND MAN_FILES ${_man})
-
- string(REGEX MATCH "^.*.5$" _man5 ${_man})
- string(REGEX MATCH "^.*.1$" _man1 ${_man})
- if(_man1)
- list(APPEND MAN1_FILES ${_man1})
- elseif(_man5)
- list(APPEND MAN5_FILES ${_man5})
- else()
- message(FATAL_ERROR "Unexpected man type")
- endif()
- endforeach()
- add_custom_target(man-roff ALL DEPENDS ${MAN_FILES})
-
- install(FILES ${MAN1_FILES} DESTINATION share/man/man1)
- install(FILES ${MAN5_FILES} DESTINATION share/man/man5)
-endif()
-
-################################################################################
-# HTML documentation
-################################################################################
-if(_html)
- set(ASCIIDOC_OPTS
- -bxhtml11
- -dmanpage
- --attribute themedir=${GREEN_DOC_DIR}
- --theme=sgreen-man)
-
- set(MAN_FILES)
- set(MAN5_FILES)
- set(MAN1_FILES)
- foreach(_name IN LISTS MAN_NAMES)
- set(_man ${CMAKE_CURRENT_BINARY_DIR}/${_name}.html)
- set(_txt ${CMAKE_CURRENT_BINARY_DIR}/${_name}.txt)
-
- add_custom_command(
- OUTPUT ${_man}
- COMMAND ${ASCIIDOC} ${ASCIIDOC_OPTS} ${_txt}
- DEPENDS man-copy ${_txt} ${GREEN_DOC_DIR}/sgreen-man.css
- WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
- COMMENT "Build HTML man page ${_man}"
- VERBATIM)
- list(APPEND MAN_FILES ${_man})
-
- string(REGEX MATCH "^.*.5.html$" _man5 ${_man})
- string(REGEX MATCH "^.*.1.html$" _man1 ${_man})
- if(_man1)
- list(APPEND MAN1_FILES ${_man1})
- elseif(_man5)
- list(APPEND MAN5_FILES ${_man5})
- else()
- message(FATAL_ERROR "Unexpected man type")
- endif()
- endforeach()
- add_custom_target(man-html ALL DEPENDS ${MAN_FILES})
-
- install(FILES ${MAN1_FILES} DESTINATION share/doc/sgreen/html)
- install(FILES ${MAN5_FILES} DESTINATION share/doc/sgreen/html)
-endif()
-
diff --git a/config.mk b/config.mk
@@ -0,0 +1,79 @@
+VERSION_MAJOR = 0
+VERSION_MINOR = 5
+VERSION_PATCH = 0
+VERSION = $(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH)
+PREFIX = /usr/local
+
+LIB_TYPE = SHARED
+#LIB_TYPE = STATIC
+
+#BUILD_TYPE = RELEASE
+BUILD_TYPE = DEBUG
+
+GREEN_ARGS_DEFAULT_VERBOSE_LEVEL = 1
+
+################################################################################
+# Tools
+################################################################################
+CC = cc
+PKG_CONFIG = pkg-config
+
+################################################################################
+# Dependencies
+################################################################################
+PCFLAGS_STATIC = --static
+PCFLAGS = $(PCFLAGS_$(LIB_TYPE))
+
+RSYS_VERSION = 0.14
+RSYS_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys)
+RSYS_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys)
+
+STARDIS_VERSION = 0.10
+STARDIS_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags stardis)
+
+# Optional (required only by suvm_voxelize tool)
+SMSH_VERSION = 0.1
+SMSH_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags smsh)
+SMSH_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs smsh)
+
+DPDC_CFLAGS = $(RSYS_CFLAGS) $(STARDIS_CFLAGS) -fopenmp
+DPDC_LIBS = $(RSYS_LIBS) -fopenmp -lm
+
+################################################################################
+# Compilation options
+################################################################################
+WFLAGS =\
+ -Wall\
+ -Wcast-align\
+ -Wconversion\
+ -Wextra\
+ -Wmissing-declarations\
+ -Wmissing-prototypes\
+ -Wshadow
+
+CFLAGS_HARDENED =\
+ -D_FORTIFY_SOURCES=2\
+ -fcf-protection=full\
+ -fstack-clash-protection\
+ -fstack-protector-strong
+
+CFLAGS_COMMON =\
+ -std=c89\
+ -pedantic\
+ -fPIE\
+ -fvisibility=hidden\
+ -fstrict-aliasing\
+ $(CFLAGS_HARDENED)\
+ $(WFLAGS)
+
+CFLAGS_DEBUG = -g $(CFLAGS_COMMON)
+CFLAGS_RELEASE = -O3 -DNDEBUG $(CFLAGS_COMMON)
+CFLAGS = $(CFLAGS_$(BUILD_TYPE))
+
+################################################################################
+# Linker options
+################################################################################
+LDFLAGS_HARDENED = -Wl,-z,relro,-z,now
+LDFLAGS_DEBUG = $(LDFLAGS_HARDENED)
+LDFLAGS_RELEASE = -s $(LDFLAGS_HARDENED)
+LDFLAGS = $(LDFLAGS_$(BUILD_TYPE)) -pie
diff --git a/make.sh b/make.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+# Copyright (C) 2020-2022, 2024 |Méso|Star> (contact@meso-star.com)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+set -e
+
+install()
+{
+ prefix=$1
+ shift 1
+
+ mkdir -p "${prefix}"
+
+ for i in "$@"; do
+ dst="${prefix}/${i##*/}"
+
+ if cmp -s "${i}" "${dst}"; then
+ printf "Up to date %s\n" "${dst}"
+ else
+ printf "Installing %s\n" "${dst}"
+ cp "${i}" "${prefix}"
+ fi
+ done
+}
+
+"$@"