tools/power/cpupower: Support building libcpupower statically

The cpupower Makefile built and installed libcpupower as a shared
library (libcpupower.so) without passing `STATIC=true`, but did not
build a static version of the library even with `STATIC=true`. (Only the
programs were static). Thus, out-of-tree programs using libcpupower
were unable to link statically against the library without having access
to intermediate object files produced during the build.

This fixes that situation by ensuring that libcpupower.a is built and
installed when `STATIC=true` is specified.

Link: https://lore.kernel.org/r/x7geegquiks3zndiavw2arihdc2rk7e2dx3lk7yxkewqii6zpg@tzjijqxyzwmu
Signed-off-by: Zuo An <zuoan.penguin@gmail.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
pull/1354/merge
Zuo An 2025-10-24 05:46:47 +00:00 committed by Shuah Khan
parent 3a86608788
commit 059835bbfa
1 changed files with 21 additions and 11 deletions

View File

@ -37,9 +37,7 @@ NLS ?= true
# cpufreq-bench benchmarking tool # cpufreq-bench benchmarking tool
CPUFREQ_BENCH ?= true CPUFREQ_BENCH ?= true
# Do not build libraries, but build the code in statically # Build the code, including libraries, statically.
# Libraries are still built, otherwise the Makefile code would
# be rather ugly.
export STATIC ?= false export STATIC ?= false
# Prefix to the directories we're installing to # Prefix to the directories we're installing to
@ -207,14 +205,25 @@ $(OUTPUT)lib/%.o: $(LIB_SRC) $(LIB_HEADERS)
$(ECHO) " CC " $@ $(ECHO) " CC " $@
$(QUIET) $(CC) $(CFLAGS) -fPIC -o $@ -c lib/$*.c $(QUIET) $(CC) $(CFLAGS) -fPIC -o $@ -c lib/$*.c
$(OUTPUT)libcpupower.so.$(LIB_VER): $(LIB_OBJS) ifeq ($(strip $(STATIC)),true)
LIBCPUPOWER := libcpupower.a
else
LIBCPUPOWER := libcpupower.so.$(LIB_VER)
endif
$(OUTPUT)$(LIBCPUPOWER): $(LIB_OBJS)
ifeq ($(strip $(STATIC)),true)
$(ECHO) " AR " $@
$(QUIET) $(AR) rcs $@ $(LIB_OBJS)
else
$(ECHO) " LD " $@ $(ECHO) " LD " $@
$(QUIET) $(CC) -shared $(CFLAGS) $(LDFLAGS) -o $@ \ $(QUIET) $(CC) -shared $(CFLAGS) $(LDFLAGS) -o $@ \
-Wl,-soname,libcpupower.so.$(LIB_MAJ) $(LIB_OBJS) -Wl,-soname,libcpupower.so.$(LIB_MAJ) $(LIB_OBJS)
@ln -sf $(@F) $(OUTPUT)libcpupower.so @ln -sf $(@F) $(OUTPUT)libcpupower.so
@ln -sf $(@F) $(OUTPUT)libcpupower.so.$(LIB_MAJ) @ln -sf $(@F) $(OUTPUT)libcpupower.so.$(LIB_MAJ)
endif
libcpupower: $(OUTPUT)libcpupower.so.$(LIB_VER) libcpupower: $(OUTPUT)$(LIBCPUPOWER)
# Let all .o files depend on its .c file and all headers # Let all .o files depend on its .c file and all headers
# Might be worth to put this into utils/Makefile at some point of time # Might be worth to put this into utils/Makefile at some point of time
@ -224,7 +233,7 @@ $(OUTPUT)%.o: %.c
$(ECHO) " CC " $@ $(ECHO) " CC " $@
$(QUIET) $(CC) $(CFLAGS) -I./lib -I ./utils -o $@ -c $*.c $(QUIET) $(CC) $(CFLAGS) -I./lib -I ./utils -o $@ -c $*.c
$(OUTPUT)cpupower: $(UTIL_OBJS) $(OUTPUT)libcpupower.so.$(LIB_VER) $(OUTPUT)cpupower: $(UTIL_OBJS) $(OUTPUT)$(LIBCPUPOWER)
$(ECHO) " CC " $@ $(ECHO) " CC " $@
ifeq ($(strip $(STATIC)),true) ifeq ($(strip $(STATIC)),true)
$(QUIET) $(CC) $(CFLAGS) $(LDFLAGS) $(UTIL_OBJS) -lrt -lpci -L$(OUTPUT) -o $@ $(QUIET) $(CC) $(CFLAGS) $(LDFLAGS) $(UTIL_OBJS) -lrt -lpci -L$(OUTPUT) -o $@
@ -269,7 +278,7 @@ update-po: $(OUTPUT)po/$(PACKAGE).pot
done; done;
endif endif
compile-bench: $(OUTPUT)libcpupower.so.$(LIB_VER) compile-bench: $(OUTPUT)$(LIBCPUPOWER)
@V=$(V) confdir=$(confdir) $(MAKE) -C bench O=$(OUTPUT) @V=$(V) confdir=$(confdir) $(MAKE) -C bench O=$(OUTPUT)
# we compile into subdirectories. if the target directory is not the # we compile into subdirectories. if the target directory is not the
@ -287,6 +296,7 @@ clean:
-find $(OUTPUT) \( -not -type d \) -and \( -name '*~' -o -name '*.[oas]' \) -type f -print \ -find $(OUTPUT) \( -not -type d \) -and \( -name '*~' -o -name '*.[oas]' \) -type f -print \
| xargs rm -f | xargs rm -f
-rm -f $(OUTPUT)cpupower -rm -f $(OUTPUT)cpupower
-rm -f $(OUTPUT)libcpupower.a
-rm -f $(OUTPUT)libcpupower.so* -rm -f $(OUTPUT)libcpupower.so*
-rm -rf $(OUTPUT)po/*.gmo -rm -rf $(OUTPUT)po/*.gmo
-rm -rf $(OUTPUT)po/*.pot -rm -rf $(OUTPUT)po/*.pot
@ -295,7 +305,11 @@ clean:
install-lib: libcpupower install-lib: libcpupower
$(INSTALL) -d $(DESTDIR)${libdir} $(INSTALL) -d $(DESTDIR)${libdir}
ifeq ($(strip $(STATIC)),true)
$(CP) $(OUTPUT)libcpupower.a $(DESTDIR)${libdir}/
else
$(CP) $(OUTPUT)libcpupower.so* $(DESTDIR)${libdir}/ $(CP) $(OUTPUT)libcpupower.so* $(DESTDIR)${libdir}/
endif
$(INSTALL) -d $(DESTDIR)${includedir} $(INSTALL) -d $(DESTDIR)${includedir}
$(INSTALL_DATA) lib/cpufreq.h $(DESTDIR)${includedir}/cpufreq.h $(INSTALL_DATA) lib/cpufreq.h $(DESTDIR)${includedir}/cpufreq.h
$(INSTALL_DATA) lib/cpuidle.h $(DESTDIR)${includedir}/cpuidle.h $(INSTALL_DATA) lib/cpuidle.h $(DESTDIR)${includedir}/cpuidle.h
@ -336,11 +350,7 @@ install-bench: compile-bench
@#DESTDIR must be set from outside to survive @#DESTDIR must be set from outside to survive
@sbindir=$(sbindir) bindir=$(bindir) docdir=$(docdir) confdir=$(confdir) $(MAKE) -C bench O=$(OUTPUT) install @sbindir=$(sbindir) bindir=$(bindir) docdir=$(docdir) confdir=$(confdir) $(MAKE) -C bench O=$(OUTPUT) install
ifeq ($(strip $(STATIC)),true)
install: all install-tools install-man $(INSTALL_NLS) $(INSTALL_BENCH)
else
install: all install-lib install-tools install-man $(INSTALL_NLS) $(INSTALL_BENCH) install: all install-lib install-tools install-man $(INSTALL_NLS) $(INSTALL_BENCH)
endif
uninstall: uninstall:
- rm -f $(DESTDIR)${libdir}/libcpupower.* - rm -f $(DESTDIR)${libdir}/libcpupower.*