Makefile 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. ARCH := avx avx2 sse
  17. TMP_DIR := output
  18. OUT_DIR := internal/native
  19. SRC_FILE := native/native.c
  20. CPU_avx := amd64
  21. CPU_avx2 := amd64
  22. CPU_sse := amd64
  23. TMPL_avx := fastint_amd64_test fastfloat_amd64_test native_amd64_test recover_amd64_test
  24. TMPL_avx2 := fastint_amd64_test fastfloat_amd64_test native_amd64_test recover_amd64_test
  25. TMPL_sse := fastint_amd64_test fastfloat_amd64_test native_amd64_test recover_amd64_test
  26. CFLAGS_avx := -msse -mno-sse4 -mavx -mpclmul -mno-avx2 -mstack-alignment=0 -DUSE_AVX=1 -DUSE_AVX2=0
  27. CFLAGS_avx2 := -msse -mno-sse4 -mavx -mpclmul -mavx2 -mstack-alignment=0 -DUSE_AVX=1 -DUSE_AVX2=1
  28. CFLAGS_sse := -msse -mno-sse4 -mno-avx -mno-avx2 -mpclmul
  29. CC_amd64 := clang
  30. ASM2ASM_amd64 := tools/asm2asm/asm2asm.py
  31. CFLAGS := -mno-red-zone
  32. CFLAGS += -target x86_64-apple-macos11
  33. CFLAGS += -fno-asynchronous-unwind-tables
  34. CFLAGS += -fno-builtin
  35. CFLAGS += -fno-exceptions
  36. CFLAGS += -fno-rtti
  37. CFLAGS += -fno-stack-protector
  38. CFLAGS += -nostdlib
  39. CFLAGS += -O3
  40. CFLAGS += -Wall -Werror
  41. NATIVE_SRC := $(wildcard native/*.h)
  42. NATIVE_SRC += $(wildcard native/*.c)
  43. .PHONY: all clean ${ARCH}
  44. define build_tmpl
  45. $(eval @arch := $(1))
  46. $(eval @tmpl := $(2))
  47. $(eval @dest := $(3))
  48. ${@dest}: ${@tmpl}
  49. mkdir -p $(dir ${@dest})
  50. echo '// Code generated by Makefile, DO NOT EDIT.' > ${@dest}
  51. echo >> ${@dest}
  52. sed -e 's/{{PACKAGE}}/${@arch}/g' ${@tmpl} >> ${@dest}
  53. endef
  54. define build_arch
  55. $(eval @cpu := $(value CPU_$(1)))
  56. $(eval @deps := $(foreach tmpl,$(value TMPL_$(1)),${OUT_DIR}/$(1)/${tmpl}.go))
  57. $(eval @asmin := ${TMP_DIR}/$(1)/native.s)
  58. $(eval @asmout := ${OUT_DIR}/$(1)/native_text_${@cpu}.go)
  59. $(eval @stubin := ${OUT_DIR}/native_${@cpu}.tmpl)
  60. $(eval @stubout := ${OUT_DIR}/$(1)/native_${@cpu}.go)
  61. $(1): ${@asmout} ${@deps}
  62. ${@asmout}: ${@stubout} ${NATIVE_SRC}
  63. mkdir -p ${TMP_DIR}/$(1)
  64. $${CC_${@cpu}} $${CFLAGS} $${CFLAGS_$(1)} -S -o ${TMP_DIR}/$(1)/native.s ${SRC_FILE}
  65. python3 $${ASM2ASM_${@cpu}} -r ${@stubout} ${TMP_DIR}/$(1)/native.s
  66. $(eval $(call \
  67. build_tmpl, \
  68. $(1), \
  69. ${@stubin}, \
  70. ${@stubout} \
  71. ))
  72. $(foreach \
  73. tmpl, \
  74. $(value TMPL_$(1)), \
  75. $(eval $(call \
  76. build_tmpl, \
  77. $(1), \
  78. ${OUT_DIR}/${tmpl}.tmpl, \
  79. ${OUT_DIR}/$(1)/${tmpl}.go \
  80. )) \
  81. )
  82. endef
  83. all: ${ARCH}
  84. clean:
  85. for arch in ${ARCH}; do \
  86. rm -vfr ${TMP_DIR}/$${arch}; \
  87. rm -vfr ${OUT_DIR}/$${arch}; \
  88. done
  89. $(foreach \
  90. arch, \
  91. ${ARCH}, \
  92. $(eval $(call build_arch,${arch})) \
  93. )