xref: /libCEED/examples/petsc/Makefile (revision d310b3d31eeeddd20725517a3a61881a36d919f0)
1# Copyright (c) 2017-2022, 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=c99 \
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 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
58multigrid.c := multigrid.c
59multigrid.o = $(multigrid.c:%.c=$(OBJDIR)/%.o)
60multigrid: $(multigrid.o) libutils.a | $(PETSc.pc) $(ceed.pc)
61	$(call quiet,LINK.o) $(CEED_LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@
62
63.SECONDEXPANSION: # to expand $$(@D)/.DIR
64%/.DIR :
65	@mkdir -p $(@D)
66	@touch $@
67
68# Quiet, color output
69quiet ?= $($(1))
70
71$(OBJDIR)/%.o : %.c | $$(@D)/.DIR
72	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<)
73
74# Rules for building the examples
75#%: %.c
76
77print: $(PETSc.pc) $(ceed.pc)
78	$(info CC      : $(CC))
79	$(info CFLAGS  : $(CFLAGS))
80	$(info CPPFLAGS: $(CPPFLAGS))
81	$(info LDFLAGS : $(LDFLAGS))
82	$(info LDLIBS  : $(LDLIBS))
83	@true
84
85clean:
86	$(RM) -r $(OBJDIR) *.vtu area bps bpsraw bpssphere multigrid libutils.a
87
88$(PETSc.pc):
89	$(if $(wildcard $@),,$(error \
90	  PETSc config not found at $@. Please set PETSC_DIR and PETSC_ARCH))
91
92.PHONY: all print clean
93
94pkgconf = $(shell pkg-config $1 | sed -e 's/^"//g' -e 's/"$$//g')
95
96-include $(src.o:%.o=%.d)
97