1# 2# Copyright (c) 2022, Google, Inc. All rights reserved 3# 4# Permission is hereby granted, free of charge, to any person obtaining 5# a copy of this software and associated documentation files 6# (the "Software"), to deal in the Software without restriction, 7# including without limitation the rights to use, copy, modify, merge, 8# publish, distribute, sublicense, and/or sell copies of the Software, 9# and to permit persons to whom the Software is furnished to do so, 10# subject to the following conditions: 11# 12# The above copyright notice and this permission notice shall be 13# included in all copies or substantial portions of the Software. 14# 15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22# 23 24# Rather than relying on the libraries provided by the host, these rules build 25# library dependencies for host tools and tests. Note that for simplicity, 26# building library dependencies recursively is not supported; any dependencies 27# for the library being built using these rules must be provided by the host. 28 29# args: 30# HOST_LIB_NAME : name of the library (required) 31# HOST_LIB_SRCS : list of source files (required) 32# HOST_LIB_FLAGS : list of flags for the compiler 33# HOST_LIB_VARIANT : suffix for the host lib to support build variants 34# HOST_INCLUDE_DIRS : list of include directories that all of the host tool/test depends on 35 36# output 37# lib$(HOST_LIB_NAME).a is appended to HOST_LIB_ARCHIVES 38 39# Validate arguments. 40ifeq ($(HOST_LIB_NAME), ) 41$(error HOST_LIB_NAME must be specified) 42endif 43 44ifeq ($(HOST_LIB_SRCS), ) 45$(error HOST_LIB_SRCS must be specified) 46endif 47 48# Build a static archive variant if requested 49ifeq (true, $(call TOBOOL,$(HOST_STATIC_LINK))) 50HOST_LIB_FLAGS += -static 51HOST_LIB_VARIANT += -static 52endif 53 54HOST_LIB_ARCHIVE := $(BUILDDIR)/host_libs/lib$(HOST_LIB_NAME)$(HOST_LIB_VARIANT).a 55 56# Guard against multiple rules for the same targets which produces make warnings 57ifndef HEADER_GUARD_HOST_LIB_$(BUILDDIR)_$(HOST_LIB_NAME)_$(HOST_LIB_VARIANT) 58HEADER_GUARD_HOST_LIB_$(BUILDDIR)_$(HOST_LIB_NAME)_$(HOST_LIB_VARIANT):=1 59 60# Compile library sources. 61GENERIC_CC := $(HOST_CC) 62GENERIC_SRCS := $(HOST_LIB_SRCS) 63GENERIC_OBJ_DIR := $(BUILDDIR)/host_libs/obj/$(HOST_LIB_NAME)$(HOST_LIB_VARIANT) 64GENERIC_FLAGS := -O1 -g -Wall -Wextra -Wno-unused-parameter -Werror $(HOST_SANITIZER_FLAGS) $(HOST_LIB_FLAGS) $(addprefix -I, $(HOST_INCLUDE_DIRS)) 65GENERIC_CFLAGS := -std=c11 -D_POSIX_C_SOURCE=200809 -Wno-missing-field-initializers 66GENERIC_CPPFLAGS := -std=c++20 $(HOST_LIBCXX_CPPFLAGS) 67GENERIC_LOG_NAME := $(HOST_LIB_NAME) 68include make/generic_compile.mk 69 70# Build static library 71$(HOST_LIB_ARCHIVE): HOST_LIB_NAME := $(HOST_LIB_NAME) 72$(HOST_LIB_ARCHIVE): $(GENERIC_OBJS) 73 @$(call ECHO,$(HOST_LIB_NAME),aring,$@) 74 @$(MKDIR) 75 $(NOECHO)$(AR) crs $@ $^ 76 @$(call ECHO_DONE_SILENT,$(HOST_LIB_NAME),aring,$@) 77 78endif 79HOST_LIB_ARCHIVES += $(HOST_LIB_ARCHIVE) 80 81# cleanup input variables 82HOST_LIB_NAME := 83HOST_LIB_SRCS := 84HOST_LIB_FLAGS := 85HOST_LIB_VARIANT := 86# cleanup internal variables 87HOST_LIB_ARCHIVE := 88GENERIC_OBJS := 89