Skip to content

Commit 54f4fa3

Browse files
committed
Robustify Rules.mk
1 parent 3074e14 commit 54f4fa3

1 file changed

Lines changed: 106 additions & 61 deletions

File tree

src/Rules.make

Lines changed: 106 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -19,73 +19,103 @@
1919

2020
ifndef MOSSCO_PREFIX
2121

22-
# 1. Checking that we're using GNU make
23-
# Of course, this command already requires gmake, so a better solution is required here
24-
ifeq ($(shell make --version | grep -c GNU),0)
25-
$(error GNU make is required)
26-
endif
22+
# 1.1. Require GNU make
23+
ifndef MAKE_VERSION
24+
$(error GNU make is required. Try invoking 'gmake' or install GNU make.)
25+
endif
2726

28-
AWK:=$(shell which gawk 2> /dev/null)
29-
ifeq ($(strip $(AWK)),)
30-
AWK:=$(shell which awk 2> /dev/null)
31-
endif
32-
ifneq ($(strip $(AWK)),)
33-
export AWK:=$(basename $(AWK))
34-
$(info Using awk ... $(AWK))
35-
endif
27+
# 1.2 Set AWK
28+
ifndef AWK
29+
AWK := $(shell command -v gawk 2>/dev/null || command -v awk 2>/dev/null)
30+
endif
31+
ifneq ($(strip $(AWK)),)
32+
export AWK
33+
$(info Using awk ... $(AWK))
34+
else
35+
$(warning awk not found; some features may not work)
36+
endif
3637

37-
CMAKE:=$(shell which cmake3 2> /dev/null)
38-
ifeq ($(strip $(CMAKE)),)
39-
CMAKE:=$(shell which cmake 2> /dev/null)
40-
endif
41-
ifneq ($(strip $(CMAKE)),)
42-
export CMAKE:=$(basename $(CMAKE))
43-
$(info Using cmake ... $(CMAKE))
44-
else
45-
$(error Cannot find cmake)
46-
endif
38+
# 1.3 Discover CMake (prefer full path). Respect pre-set CMAKE.
39+
ifndef CMAKE
40+
CMAKE := $(shell command -v cmake 2>/dev/null || command -v cmake3 2>/dev/null)
41+
endif
42+
ifeq ($(strip $(CMAKE)),)
43+
$(error Cannot find CMake. Please install cmake >= 3.5 and ensure it is on PATH)
44+
else
45+
export CMAKE
46+
$(info Using cmake ... $(CMAKE))
47+
# Optional: check minimum version 3.5
48+
CMAKE_VERSION := $(shell "$(CMAKE)" --version 2>/dev/null | sed -n '1s/.* \([0-9][0-9.]*\)$$/\1/p')
49+
CMAKE_VER_MAJOR := $(firstword $(subst ., ,$(CMAKE_VERSION)))
50+
CMAKE_VER_MINOR := $(word 2,$(subst ., ,$(CMAKE_VERSION)))
51+
CMAKE_OK := $(shell [ -n "$(CMAKE_VERSION)" ] && { [ $(CMAKE_VER_MAJOR) -gt 3 ] || { [ $(CMAKE_VER_MAJOR) -eq 3 ] && [ $(CMAKE_VER_MINOR) -ge 5 ]; }; } && echo yes || echo no)
52+
ifeq ($(CMAKE_OK),no)
53+
$(warning Detected CMake $(CMAKE_VERSION); recommend >= 3.5 for policy support)
54+
endif
55+
endif
4756

48-
export MOSSCO_OBJC:=false
49-
OBJC=$(shell which objconv 2> /dev/null)
50-
ifeq ($(strip $(OBJC)),)
51-
OBJC=$(shell which gobjcopy 2> /dev/null)
52-
endif
53-
ifeq ($(strip $(OBJC)),)
54-
OBJC=$(shell which objcopy 2> /dev/null)
55-
endif
56-
ifneq ($(strip $(OBJC)),)
57-
MOSSCO_OBJC:=$(shell basename $(OBJC))
58-
$(info Using objcopy ... $(MOSSCO_OBJC))
59-
else
60-
$(info Using objcopy ... no)
61-
endif
57+
# 1.4 Discover GNU objcopy (prefer full path). Respect pre-set OBJCOPY.
58+
ifndef OBJCOPY
59+
# gobjcopy appears on macOS (Homebrew binutils), objcopy elsewhere
60+
OBJCOPY := $(shell command -v objcopy 2>/dev/null || command -v gobjcopy 2>/dev/null)
61+
endif
62+
ifeq ($(strip $(OBJCOPY)),)
63+
export MOSSCO_HAS_OBJCOPY := false
64+
$(info Using objcopy ... not found)
65+
else
66+
export MOSSCO_HAS_OBJCOPY := true
67+
export OBJCOPY
68+
$(info Using objcopy ... $(OBJCOPY))
69+
endif
70+
ifeq ($(MOSSCO_HAS_OBJCOPY),false)
71+
ifndef OBJCONV
72+
OBJCONV := $(shell command -v objconv 2>/dev/null)
73+
endif
74+
ifneq ($(strip $(OBJCONV)),)
75+
export MOSSCO_HAS_OBJCONV := true
76+
$(info Using objconv ... $(OBJCONV))
77+
endif
78+
endif
6279

63-
export MOSSCO_GIT:=false
64-
ifneq ($(wildcard $(shell which git)),)
65-
MOSSCO_GIT:=true
66-
export MOSSCO_GIT_VERSION:=$(shell git --version |cut -f3 -d" ")
67-
export MOSSCO_GIT_VERSION_MAJOR:=$(shell git --version |cut -f3 -d" "|cut -f1 -d.)
68-
ifeq ($(MOSSCO_GIT_VERSION_MAJOR),1)
69-
$(warning Consider upgrading git to version 2)
70-
endif
71-
else
72-
$(warning Consider installing git)
73-
endif
74-
$(info Using git ... $(shell which git) ($(MOSSCO_GIT_VERSION)))
80+
ifndef GIT
81+
GIT := $(shell command -v git 2>/dev/null)
82+
endif
7583

76-
# System-dependent flags
77-
ifeq ($(shell hostname),rznp0023)
78-
export ARFLAGS:=rv
79-
export AR:=ar
80-
$(warning use changed ARFLAGS=rvU)
81-
endif
84+
# 1.5 Discover git (prefer full path). Respect pre-set GIT.
85+
ifeq ($(strip $(GIT)),)
86+
export MOSSCO_GIT := false
87+
$(warning git not found; VCS metadata features will be disabled)
88+
else
89+
export MOSSCO_GIT := true
90+
export GIT
91+
# Extract version string (e.g. "2.43.0")
92+
GIT_VERSION := $(shell "$(GIT)" --version 2>/dev/null | sed -n 's/.* \([0-9][0-9.]*\)$$/\1/p')
93+
ifneq ($(strip $(GIT_VERSION)),)
94+
export MOSSCO_GIT_VERSION := $(GIT_VERSION)
95+
export MOSSCO_GIT_VERSION_MAJOR := $(firstword $(subst ., ,$(GIT_VERSION)))
96+
$(info Using git ... $(GIT) ($(MOSSCO_GIT_VERSION)))
97+
# Warn for very old git (major < 2)
98+
GIT_OK := $(shell [ "$(firstword $(subst ., ,$(GIT_VERSION)))" -ge 2 ] 2>/dev/null && echo yes || echo no)
99+
ifeq ($(GIT_OK),no)
100+
$(warning Consider upgrading git to version 2 or newer)
101+
endif
102+
else
103+
$(info Using git ... $(GIT))
104+
endif
105+
endif
106+
# Portable ar setup: respect pre-set AR/ARFLAGS, otherwise discover
107+
ifndef AR
108+
AR := $(shell command -v ar 2>/dev/null || command -v llvm-ar 2>/dev/null || command -v gar 2>/dev/null)
109+
endif
110+
ifeq ($(strip $(AR)),)
111+
$(error 'ar' not found; please install binutils (ar) or llvm-ar and ensure it is on PATH)
112+
endif
82113

83-
ifeq ($(shell hostname),KSEZ8002)
84-
export ARFLAGS:=rvU
85-
export AR:=ar
86-
$(warning use changed ARFLAGS=rvU)
87-
endif
88-
$(info Using ar ... $(AR) -$(ARFLAGS))
114+
# Safe, portable default flags: rcs (replace/create, write index)
115+
ARFLAGS ?= rcs
116+
117+
export AR ARFLAGS
118+
$(info Using ar ... $(AR) -$(ARFLAGS))
89119

90120
ifeq ($(MOSSCO_INSTALL_PREFIX),)
91121
export MOSSCO_INSTALL_PREFIX:=$(MOSSCO_DIR)
@@ -116,6 +146,21 @@ else
116146

117147
$(info Using ESMFMKFILE ... $(ESMFMKFILE))
118148

149+
# Compilers from ESMF
150+
ifdef ESMF_F90COMPILER
151+
export F90 := $(ESMF_F90COMPILER)
152+
export FC := $(ESMF_F90COMPILER)
153+
else
154+
$(error ESMF_F90COMPILER not defined in $(ESMFMKFILE))
155+
endif
156+
ifdef ESMF_CXXCOMPILER
157+
export CXX := $(ESMF_CXXCOMPILER)
158+
endif
159+
ifdef ESMF_CCOMPILER
160+
export CC := $(ESMF_CCOMPILER)
161+
endif
162+
163+
119164
# Find the communicator and determine whether this is parallel device, this
120165
# is still buggy with mpiifort and needs improvement
121166
ESMF_COMM:=$(strip $(shell grep '^. ESMF_COMM:' $(ESMFMKFILE) | cut -d':' -f2-))

0 commit comments

Comments
 (0)