Makefile (6651B)
1 # Copyright (C) 2022, 2025, 2026 |Méso|Star> (contact@meso-star.com) 2 # Copyright (C) 2025, 2026 Université de Lorraine 3 # Copyright (C) 2022 Centre National de la Recherche Scientifique 4 # Copyright (C) 2022 Université Paul Sabatier 5 # 6 # This program is free software: you can redistribute it and/or modify 7 # it under the terms of the GNU General Public License as published by 8 # the Free Software Foundation, either version 3 of the License, or 9 # (at your option) any later version. 10 # 11 # This program is distributed in the hope that it will be useful, 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 # GNU General Public License for more details. 15 # 16 # You should have received a copy of the GNU General Public License 17 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 19 .POSIX: 20 .SUFFIXES: # Clean up default inference rules 21 22 include config.mk 23 24 LIBNAME_STATIC = libshtr.a 25 LIBNAME_SHARED = libshtr.so 26 LIBNAME = $(LIBNAME_$(LIB_TYPE)) 27 28 default: library 29 all: library tests utils 30 31 ################################################################################ 32 # Library building 33 ################################################################################ 34 SRC = \ 35 src/shtr.c \ 36 src/shtr_isotope_metadata.c \ 37 src/shtr_line_list.c \ 38 src/shtr_param.c 39 OBJ = $(SRC:.c=.o) 40 DEP = $(SRC:.c=.d) 41 42 CFLAGS_LIB = -std=c99 $(CFLAGS_SO) $(INCS) -DSHTR_SHARED_BUILD 43 LDFLAGS_LIB = $(LDFLAGS_SO) $(LIBS) 44 45 library: .config $(DEP) 46 @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) \ 47 $$(if [ -n "$(LIBNAME)" ]; then\ 48 echo "$(LIBNAME)";\ 49 else\ 50 echo "$(LIBNAME_SHARED)";\ 51 fi) 52 53 $(DEP) $(OBJ): config.mk 54 55 $(LIBNAME_SHARED): $(OBJ) 56 $(CC) $(CFLAGS_LIB) -o $@ $(OBJ) $(LDFLAGS_LIB) 57 58 $(LIBNAME_STATIC): libshtr.o 59 $(AR) -rc $@ $? 60 $(RANLIB) $@ 61 62 libshtr.o: $(OBJ) 63 $(LD) -r $(OBJ) -o $@ 64 $(OBJCOPY) $(OCPFLAGS) $@ 65 66 .config: config.mk 67 $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys 68 echo "config done" > $@ 69 70 .SUFFIXES: .c .d .o 71 .c.d: 72 @$(CC) $(CFLAGS_LIB) -MM -MT "$(@:.d=.o) $@" $< -MF $@ 73 74 .c.o: 75 $(CC) $(CFLAGS_LIB) -c $< -o $@ 76 77 ################################################################################ 78 # Utils 79 ################################################################################ 80 UTIL_SRC = src/shtr_main.c 81 UTIL_OBJ = $(UTIL_SRC:.c=.o) 82 UTIL_DEP = $(UTIL_SRC:.c=.d) 83 84 PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG) 85 86 INCS_UTIL = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys shtr-local) 87 LIBS_UTIL = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys shtr-local) 88 89 CFLAGS_UTIL = -std=c89 $(CFLAGS_EXE) $(INCS_UTIL) 90 LDFLAGS_UTIL = $(LDFLAGS_EXE) $(LIBS_UTIL) 91 92 utils: library $(UTIL_DEP) 93 @$(MAKE) -fMakefile \ 94 $$(for i in $(UTIL_DEP); do printf -- '-f%s\n' "$${i}"; done) \ 95 shtr 96 97 shtr: config.mk shtr-local.pc src/shtr_main.o $(LIBNAME) 98 $(CC) $(CFLAGS_UTIL) -o $@ src/shtr_main.o $(LDFLAGS_UTIL) 99 100 $(UTIL_DEP): config.mk shtr-local.pc 101 $(CC) $(CFLAGS_UTIL) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ 102 103 $(UTIL_OBJ): config.mk shtr-local.pc 104 $(CC) $(CFLAGS_UTIL) -c $(@:.o=.c) -o $@ 105 106 ################################################################################ 107 # Installation 108 ################################################################################ 109 pkg: 110 sed -e 's#@PREFIX@#$(PREFIX)#g' \ 111 -e 's#@VERSION@#$(VERSION)#g' \ 112 -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g' \ 113 shtr.pc.in > shtr.pc 114 115 shtr-local.pc: shtr.pc.in 116 sed -e '1d'\ 117 -e 's#^includedir=.*#includedir=./src/#' \ 118 -e 's#^libdir=.*#libdir=./#' \ 119 -e 's#@VERSION@#$(VERSION)#g' \ 120 -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g' \ 121 shtr.pc.in > $@ 122 123 install: library pkg utils 124 install() { mode="$$1"; prefix="$$2"; shift 2; \ 125 mkdir -p "$${prefix}"; \ 126 cp "$$@" "$${prefix}"; \ 127 printf '%s\n' "$${@}" | while read -r i; do \ 128 chmod "$${mode}" "$${prefix}/$${i##*/}"; \ 129 done; \ 130 }; \ 131 if [ "$(LIB_TYPE)" = "STATIC" ]; then mode=644; else mode=755; fi; \ 132 install "$${mode}" "$(DESTDIR)$(LIBPREFIX)" $(LIBNAME); \ 133 install 644 "$(DESTDIR)$(LIBPREFIX)/pkgconfig" shtr.pc; \ 134 install 755 "$(DESTDIR)$(BINPREFIX)" shtr; \ 135 install 644 "$(DESTDIR)$(INCPREFIX)/star" src/shtr.h; \ 136 install 644 "$(DESTDIR)$(MANPREFIX)/man1" doc/shtr.1; \ 137 install 644 "$(DESTDIR)$(PREFIX)/share/doc/star-hitran" COPYING README.md 138 139 uninstall: 140 rm -f "$(DESTDIR)$(LIBPREFIX)/$(LIBNAME)" 141 rm -f "$(DESTDIR)$(LIBPREFIX)/pkgconfig/shtr.pc" 142 rm -f "$(DESTDIR)$(BINPREFIX)/shtr" 143 rm -f "$(DESTDIR)$(INCPREFIX)/star/shtr.h" 144 rm -f "$(DESTDIR)$(MANPREFIX)/man1/shtr.1" 145 rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-hitran/COPYING" 146 rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-hitran/README.md" 147 148 clean: clean_test 149 rm -f $(OBJ) $(DEP) $(LIBNAME) 150 rm -f $(UTIL_OBJ) $(UTIL_DEP) shtr 151 rm -f .config libshtr.o shtr.pc shtr-local.pc 152 153 lint: 154 mandoc -Tlint doc/shtr.1 155 156 ################################################################################ 157 # Tests 158 ################################################################################ 159 TEST_SRC = \ 160 src/test_shtr.c \ 161 src/test_shtr_isotope_metadata.c \ 162 src/test_shtr_lines.c \ 163 src/test_shtr_molecule_id.c 164 TEST_OBJ = $(TEST_SRC:.c=.o) 165 TEST_DEP = $(TEST_SRC:.c=.d) 166 TEST_TGT = $(TEST_SRC:.c=.t) 167 168 INCS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys shtr-local) 169 LIBS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys shtr-local) 170 171 CFLAGS_TEST = -std=c89 $(CFLAGS_EXE) $(INCS_TEST) 172 LDFLAGS_TEST = $(LDFLAGS_EXE) $(LIBS_TEST) -lm 173 174 tests: library $(TEST_DEP) $(TEST_TGT) 175 @$(MAKE) -fMakefile \ 176 $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) \ 177 $$(for i in $(TEST_TGT); do echo -f"$${i}"; done) \ 178 test_list 179 180 $(TEST_TGT): 181 @{ \ 182 exe="$$(basename "$@" ".t")"; \ 183 printf '%s: %s\n' "$${exe}" $(@:.t=.o); \ 184 printf 'test_list: %s\n' "$${exe}"; \ 185 } > $@ 186 187 $(TEST_DEP): config.mk shtr-local.pc 188 @$(CC) $(CFLAGS_TEST) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ 189 190 $(TEST_OBJ): config.mk shtr-local.pc 191 $(CC) $(CFLAGS_TEST) -c $(@:.o=.c) -o $@ 192 193 test_shtr \ 194 test_shtr_isotope_metadata \ 195 test_shtr_lines \ 196 test_shtr_molecule_id \ 197 : config.mk shtr-local.pc $(LIBNAME) 198 $(CC) $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST) 199 200 clean_test: 201 rm -f $(TEST_DEP) $(TEST_OBJ) $(TEST_TGT) 202 rm -f test_isotope_metadata.txt test_lines.txt 203 for i in $(TEST_SRC); do rm -f "$$(basename "$${i}" ".c")"; done 204 205 test: tests 206 @err=0; \ 207 for i in $(TEST_SRC); do \ 208 test="$$(basename "$${i}" ".c")"; \ 209 printf '%s' "$${test}"; \ 210 if "./$${test}" > /dev/null 2>&1; then \ 211 printf '\n'; \ 212 else \ 213 printf ': error %s\n' "$$?"; \ 214 err=$$((err+1)); \ 215 fi \ 216 done; \ 217 [ "$${err}" -eq 0 ]