xref: /libCEED/examples/petsc/Makefile (revision 9ba83ac0e4b1fca39d6fa6737a318a9f0cbc172d)
1# Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors
2# All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# This file is part of CEED:  http://github.com/ceed
7
8COMMON ?= ../../common.mk
9-include $(COMMON)
10
11# Note: PETSC_ARCH can be undefined or empty for installations which do not use
12#       PETSC_ARCH - for example when using PETSc installed through Spack.
13PETSc.pc := $(PETSC_DIR)/$(PETSC_ARCH)/lib/pkgconfig/PETSc.pc
14CEED_DIR ?= ../..
15ceed.pc := $(CEED_DIR)/lib/pkgconfig/ceed.pc
16
17CC = $(call pkgconf, --variable=ccompiler $(PETSc.pc) $(ceed.pc))
18CFLAGS = -std=c11 \
19  $(call pkgconf, --variable=cflags_extra $(PETSc.pc)) \
20  $(call pkgconf, --cflags-only-other $(PETSc.pc)) \
21  $(OPT)
22CPPFLAGS = $(call pkgconf, --cflags-only-I $(PETSc.pc) $(ceed.pc)) \
23  $(call pkgconf, --variable=cflags_dep $(PETSc.pc))
24LDFLAGS = $(call pkgconf, --libs-only-L --libs-only-other $(PETSc.pc) $(ceed.pc))
25LDFLAGS += $(patsubst -L%, $(call pkgconf, --variable=ldflag_rpath $(PETSc.pc))%, $(call pkgconf, --libs-only-L $(PETSc.pc) $(ceed.pc)))
26LDLIBS = $(call pkgconf, --libs-only-l $(PETSc.pc) $(ceed.pc)) -lm
27
28OBJDIR := build
29SRCDIR := src
30
31all: area bps bpsraw bpssphere dmswarm multigrid
32
33utils.c := $(sort $(wildcard $(SRCDIR)/*.c))
34utils.o = $(utils.c:%.c=$(OBJDIR)/%.o)
35libutils.a: $(utils.o)
36	$(call quiet,AR) $(ARFLAGS) $@ $^
37
38area.c := area.c
39area.o = $(area.c:%.c=$(OBJDIR)/%.o)
40area: $(area.o) libutils.a | $(PETSc.pc) $(ceed.pc)
41	$(call quiet,LINK.o) $(CEED_LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@
42
43bps.c := bps.c
44bps.o = $(bps.c:%.c=$(OBJDIR)/%.o)
45bps: $(bps.o) libutils.a | $(PETSc.pc) $(ceed.pc)
46	$(call quiet,LINK.o) $(CEED_LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@
47
48bpsraw.c := bpsraw.c
49bpsraw.o = $(bpsraw.c:%.c=$(OBJDIR)/%.o)
50bpsraw: $(bpsraw.o) libutils.a | $(PETSc.pc) $(ceed.pc)
51	$(call quiet,LINK.o) $(CEED_LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@
52
53bpssphere.c := bpssphere.c
54bpssphere.o = $(bpssphere.c:%.c=$(OBJDIR)/%.o)
55bpssphere: $(bpssphere.o) libutils.a | $(PETSc.pc) $(ceed.pc)
56	$(call quiet,LINK.o) $(CEED_LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@
57
58bpsswarm.c := bpsswarm.c
59bpsswarm.o = $(bpsswarm.c:%.c=$(OBJDIR)/%.o)
60bpsswarm: $(bpsswarm.o) libutils.a | $(PETSc.pc) $(ceed.pc)
61	$(call quiet,LINK.o) $(CEED_LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@
62
63
64dmswarm.c := dmswarm.c
65dmswarm.o = $(dmswarm.c:%.c=$(OBJDIR)/%.o)
66dmswarm: $(dmswarm.o) libutils.a | $(PETSc.pc) $(ceed.pc)
67	$(call quiet,LINK.o) $(CEED_LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@
68
69multigrid.c := multigrid.c
70multigrid.o = $(multigrid.c:%.c=$(OBJDIR)/%.o)
71multigrid: $(multigrid.o) libutils.a | $(PETSc.pc) $(ceed.pc)
72	$(call quiet,LINK.o) $(CEED_LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@
73
74.SECONDEXPANSION: # to expand $$(@D)/.DIR
75%/.DIR :
76	@mkdir -p $(@D)
77	@touch $@
78
79# Quiet, color output
80quiet ?= $($(1))
81
82$(OBJDIR)/%.o : %.c | $$(@D)/.DIR
83	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<)
84
85# Rules for building the examples
86#%: %.c
87
88print: $(PETSc.pc) $(ceed.pc)
89	$(info CC      : $(CC))
90	$(info CFLAGS  : $(CFLAGS))
91	$(info CPPFLAGS: $(CPPFLAGS))
92	$(info LDFLAGS : $(LDFLAGS))
93	$(info LDLIBS  : $(LDLIBS))
94	@true
95
96clean:
97	$(RM) -r $(OBJDIR) *.vtu area bps bpsraw bpssphere dmswarm multigrid libutils.a
98
99$(PETSc.pc):
100	$(if $(wildcard $@),,$(error \
101	  PETSc config not found at $@. Please set PETSC_DIR and PETSC_ARCH))
102
103.PHONY: all print clean
104
105pkgconf = $(shell pkg-config $1 | sed -e 's/^"//g' -e 's/"$$//g')
106
107-include $(src.o:%.o=%.d)
108