github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/Makefile (about)

     1  #
     2  # Copyright 2021 ByteDance Inc.
     3  #
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  #
     8  #     http://www.apache.org/licenses/LICENSE-2.0
     9  #
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  #
    16  
    17  ARCH			:= avx avx2 sse
    18  TMP_DIR			:= output
    19  OUT_DIR			:= internal/native
    20  SRC_FILE		:= native/native.c
    21  
    22  CPU_avx			:= amd64
    23  CPU_avx2		:= amd64
    24  CPU_sse  		:= amd64
    25  
    26  TMPL_avx		:= fastint_amd64_test fastfloat_amd64_test native_amd64_test recover_amd64_test
    27  TMPL_avx2		:= fastint_amd64_test fastfloat_amd64_test native_amd64_test recover_amd64_test
    28  TMPL_sse 		:= fastint_amd64_test fastfloat_amd64_test native_amd64_test recover_amd64_test
    29  
    30  CFLAGS_avx		:= -msse -mno-sse4 -mavx -mpclmul -mno-avx2 -mstack-alignment=0 -DUSE_AVX=1 -DUSE_AVX2=0
    31  CFLAGS_avx2		:= -msse -mno-sse4 -mavx -mpclmul -mavx2 -mstack-alignment=0 -DUSE_AVX=1 -DUSE_AVX2=1 
    32  CFLAGS_sse		:= -msse -mno-sse4 -mno-avx -mno-avx2 -mpclmul
    33  
    34  CC_amd64		:= clang
    35  ASM2ASM_amd64	:= tools/asm2asm/asm2asm.py
    36  
    37  CFLAGS			:= -mno-red-zone
    38  CFLAGS			+= -target x86_64-apple-macos11
    39  CFLAGS			+= -fno-asynchronous-unwind-tables
    40  CFLAGS			+= -fno-builtin
    41  CFLAGS			+= -fno-exceptions
    42  CFLAGS			+= -fno-rtti
    43  CFLAGS			+= -fno-stack-protector
    44  CFLAGS			+= -nostdlib
    45  CFLAGS			+= -O3
    46  CFLAGS			+= -Wall -Werror
    47  
    48  NATIVE_SRC		:= $(wildcard native/*.h)
    49  NATIVE_SRC		+= $(wildcard native/*.c)
    50  
    51  .PHONY: all clean ${ARCH}
    52  
    53  define build_tmpl
    54  	$(eval @arch := $(1))
    55  	$(eval @tmpl := $(2))
    56  	$(eval @dest := $(3))
    57  
    58  ${@dest}: ${@tmpl}
    59  	mkdir -p $(dir ${@dest})
    60  	echo '// Code generated by Makefile, DO NOT EDIT.' > ${@dest}
    61  	echo >> ${@dest}
    62  	sed -e 's/{{PACKAGE}}/${@arch}/g' ${@tmpl} >> ${@dest}
    63  endef
    64  
    65  define build_arch
    66  	$(eval @cpu		:= $(value CPU_$(1)))
    67  	$(eval @deps	:= $(foreach tmpl,$(value TMPL_$(1)),${OUT_DIR}/$(1)/${tmpl}.go))
    68  	$(eval @asmin	:= ${TMP_DIR}/$(1)/native.s)
    69  	$(eval @asmout	:= ${OUT_DIR}/$(1)/native_text_${@cpu}.go)
    70  	$(eval @stubin	:= ${OUT_DIR}/native_${@cpu}.tmpl)
    71  	$(eval @stubout	:= ${OUT_DIR}/$(1)/native_${@cpu}.go)
    72  
    73  $(1): ${@asmout} ${@deps}
    74  
    75  ${@asmout}: ${@stubout} ${NATIVE_SRC}
    76  	mkdir -p ${TMP_DIR}/$(1)
    77  	$${CC_${@cpu}} $${CFLAGS} $${CFLAGS_$(1)} -S -o ${TMP_DIR}/$(1)/native.s ${SRC_FILE}
    78  	python3 $${ASM2ASM_${@cpu}} -r ${@stubout} ${TMP_DIR}/$(1)/native.s
    79  
    80  $(eval $(call 	\
    81  	build_tmpl,	\
    82  	$(1),		\
    83  	${@stubin},	\
    84  	${@stubout}	\
    85  ))
    86  
    87  $(foreach 							\
    88  	tmpl,							\
    89  	$(value TMPL_$(1)),				\
    90  	$(eval $(call 					\
    91  		build_tmpl,					\
    92  		$(1),						\
    93  		${OUT_DIR}/${tmpl}.tmpl,	\
    94  		${OUT_DIR}/$(1)/${tmpl}.go	\
    95  	))								\
    96  )
    97  endef
    98  
    99  all: ${ARCH}
   100  
   101  clean:
   102  	for arch in ${ARCH}; do \
   103  		rm -vfr ${TMP_DIR}/$${arch}; \
   104  		rm -vfr ${OUT_DIR}/$${arch}; \
   105  	done
   106  
   107  $(foreach 								\
   108  	arch,								\
   109  	${ARCH},							\
   110  	$(eval $(call build_arch,${arch}))	\
   111  )