LICENSE   = BSD
VERSION   = 12.0.0
DOWNLOADS = libc.archive openlibm.archive

D = src/lib/libc

URL(libc) = http://ftp.freebsd.org/pub/FreeBSD/releases/amd64/12.0-RELEASE/src.txz
SHA(libc) = 0da393ac2174168a71c1c527d1453e07372295187d05c288250800cb152a889b
DIR(libc) = $(D)

OWNER := JuliaMath
REPO  := openlibm
REV   := a96f0740e32c3d8aaa0a34c3988201018dfa90ce

URL(openlibm) := https://github.com/$(OWNER)/$(REPO)/archive/$(REV).tar.gz
SHA(openlibm) := a9c7d4450c3e9a8b92ec1e20c7a9fd2682e900743dff37f163c9a9ac68b4f471
DIR(openlibm) := src/lib/openlibm

TAR_OPT(libc) = --strip-components=2

TAR_OPT(libc) += usr/src/contrib/gdtoa
TAR_OPT(libc) += usr/src/contrib/libc-vis
TAR_OPT(libc) += usr/src/contrib/tzcode/stdtime
TAR_OPT(libc) += usr/src/include
TAR_OPT(libc) += usr/src/lib/libc
TAR_OPT(libc) += usr/src/lib/msun
TAR_OPT(libc) += usr/src/sys/amd64
TAR_OPT(libc) += usr/src/sys/arm
TAR_OPT(libc) += usr/src/sys/arm64
TAR_OPT(libc) += usr/src/sys/bsm
TAR_OPT(libc) += usr/src/sys/crypto/chacha20
TAR_OPT(libc) += usr/src/sys/i386
TAR_OPT(libc) += usr/src/sys/libkern
TAR_OPT(libc) += usr/src/sys/net
TAR_OPT(libc) += usr/src/sys/netinet
TAR_OPT(libc) += usr/src/sys/netinet6
TAR_OPT(libc) += usr/src/sys/riscv
TAR_OPT(libc) += usr/src/sys/rpc
TAR_OPT(libc) += usr/src/sys/sys
TAR_OPT(libc) += usr/src/sys/vm
TAR_OPT(libc) += usr/src/sys/x86

#
# Check for tools
#
$(call check_tool,flex)
$(call check_tool,bison)
$(call check_tool,rpcgen)

PATCHES := $(sort $(wildcard $(REP_DIR)/$(D)/patches/*.patch))


#
# Generic headers
#

DIRS := include/libc include/openlibm

DIR_CONTENT(include/openlibm) = src/lib/openlibm/include/*

DIR_CONTENT(include/libc) := \
    $(addprefix $(D)/include/,\
                strings.h limits.h string.h ctype.h _ctype.h \
                stdlib.h stdio.h signal.h unistd.h wchar.h time.h sysexits.h \
                resolv.h wctype.h locale.h langinfo.h regex.h paths.h ieeefp.h \
                inttypes.h fstab.h netdb.h ar.h memory.h res_update.h \
                netconfig.h ifaddrs.h pthread.h err.h getopt.h search.h \
                varargs.h stddef.h stdbool.h assert.h monetary.h printf.h \
                libgen.h dirent.h dlfcn.h link.h fmtmsg.h fnmatch.h fts.h ftw.h \
                db.h grp.h nsswitch.h pthread_np.h pwd.h ttyent.h \
                stringlist.h glob.h a.out.h elf-hints.h nlist.h spawn.h \
                readpassphrase.h setjmp.h elf.h ulimit.h utime.h wordexp.h \
                complex.h semaphore.h uchar.h iconv.h termios.h \
                xlocale.h runetype.h)  \
    $(addprefix $(D)/sys/sys/,\
                syslog.h fcntl.h stdint.h sched.h ktrace.h \
                _semaphore.h ucontext.h) \
    $(D)/sys/sys/errno.h \
    $(D)/lib/msun/src/math.h \
    $(D)/contrib/libc-vis/vis.h

DIRS += include/libc/rpc
DIR_CONTENT(include/libc/rpc) := \
    $(addprefix $(D)/include/rpc/,\
                rpc.h xdr.h auth.h clnt_stat.h clnt.h clnt_soc.h rpc_msg.h \
                auth_unix.h auth_des.h svc.h svc_soc.h svc_auth.h pmap_clnt.h \
                pmap_prot.h rpcb_clnt.h rpcent.h des_crypt.h des.h nettype.h \
                rpcsec_gss.h raw.h rpc_com.h) \
    $(D)/include/rpc/rpcb_prot.h

DIRS += include/libc/rpcsvc
DIR_CONTENT(include/libc/rpcsvc) := \
    $(addprefix $(D)/include/rpcsvc/,\
                yp_prot.h nis.h ypclnt.h nis_tags.h nislib.h)

DIRS += include/libc/gssapi
DIR_CONTENT(include/libc/gssapi) := $(D)/include/gssapi/gssapi.h

DIRS += include/libc/arpa
DIR_CONTENT(include/libc/arpa) := \
    $(addprefix $(D)/include/arpa/,\
                inet.h ftp.h nameser.h nameser_compat.h telnet.h tftp.h)

DIRS += include/libc/vm
DIR_CONTENT(include/libc/vm) := \
    $(addprefix $(D)/sys/vm/, vm_param.h vm.h pmap.h)

DIRS += include/libc/net
DIR_CONTENT(include/libc/net) := \
    $(addprefix $(D)/sys/net/, if.h if_dl.h if_tun.h if_types.h \
                radix.h route.h ethernet.h if_arp.h vnet.h)

DIRS += include/libc/netinet
DIR_CONTENT(include/libc/netinet) := \
    $(addprefix $(D)/sys/netinet/, in.h in_systm.h ip.h tcp.h \
                udp.h ip_icmp.h if_ether.h tcp_fsm.h)

DIRS += include/libc/netinet6
DIR_CONTENT(include/libc/netinet6) := $(D)/sys/netinet6/in6.h

DIRS += include/libc/bsm
DIR_CONTENT(include/libc/bsm) := $(D)/sys/bsm/audit.h

DIRS += include/libc/sys/rpc
DIR_CONTENT(include/libc/sys/rpc) := $(D)/sys/rpc/types.h

DIRS += include/libc/sys
DIR_CONTENT(include/libc/sys) := \
    $(addprefix $(D)/sys/sys/,\
                _types.h limits.h cdefs.h _null.h types.h _pthreadtypes.h \
                syslimits.h select.h _sigset.h _timeval.h timespec.h \
                _timespec.h stat.h signal.h unistd.h time.h param.h stdint.h \
                event.h eventhandler.h disk.h errno.h poll.h queue.h mman.h \
                stddef.h sysctl.h uio.h _iovec.h ktrace.h ioctl.h ttycom.h \
                ioccom.h filio.h sockio.h wait.h file.h fcntl.h resource.h \
                disklabel.h link_elf.h endian.h mount.h ucred.h dirent.h \
                cpuset.h socket.h un.h ttydefaults.h imgact_aout.h elf32.h \
                elf64.h elf_generic.h elf_common.h nlist_aout.h ipc.h sem.h \
                exec.h _lock.h _mutex.h statvfs.h ucontext.h syslog.h times.h \
                utsname.h elf.h mtio.h _stdint.h atomic_common.h _ucontext.h \
                _cpuset.h _bitset.h bitset.h _stdarg.h _uio.h auxv.h random.h \
                _sockaddr_storage.h termios.h _termios.h _umtx.h kerneldump.h \
                conf.h disk_zone.h counter.h)

DIRS += include/libc/sys/disk
DIR_CONTENT(include/libc/sys/disk) := $(D)/sys/sys/disk/*h

DIRS += include/libc/xlocale
DIR_CONTENT(include/libc/xlocale) := $(D)/include/xlocale/*.h

#
# CPU-architecture-specific headers
#
# The 'common_include_*_content' functions take the CPU architecture as first
# argument.
#

common_include_libc_arch_content = \
    $(addprefix $(D)/sys/$1/include/, stdarg.h float.h) \
    $(addprefix $(D)/lib/libc/$2/, arith.h _fpmath.h SYS.h gd_qnan.h)

common_include_libc_arch_machine_content = \
    $(addprefix $(D)/sys/$1/include/,\
                _types.h endian.h _limits.h signal.h trap.h _stdint.h \
                sysarch.h ieeefp.h frame.h vm.h \
                cpufunc.h vmparam.h atomic.h elf.h exec.h reloc.h pmap.h \
                ucontext.h setjmp.h asm.h param.h _inttypes.h _align.h \
                float.h)

#
# x86-specific headers
#
DIRS += include/spec/x86/libc
DIR_CONTENT(include/spec/x86/libc) := \
    $(D)/lib/msun/x86/fenv.h

DIRS += include/libc/x86
DIR_CONTENT(include/libc/x86) := \
	$(D)/sys/x86/include/*.h

#
# i386-specific headers
#
DIRS += include/spec/x86_32/libc
DIR_CONTENT(include/spec/x86_32/libc) := \
    $(call common_include_libc_arch_content,i386,i386)

DIRS += include/spec/x86_32/libc/machine
DIR_CONTENT(include/spec/x86_32/libc/machine) := \
    $(call common_include_libc_arch_machine_content,i386) \
    $(addprefix $(D)/sys/i386/include/, specialreg.h npx.h sigframe.h)


#
# AMD64-specific headers
#
DIRS += include/spec/x86_64/libc
DIR_CONTENT(include/spec/x86_64/libc) := \
    $(call common_include_libc_arch_content,amd64,amd64)

DIRS += include/spec/x86_64/libc/machine
DIR_CONTENT(include/spec/x86_64/libc/machine) := \
    $(call common_include_libc_arch_machine_content,amd64) \
    $(addprefix $(D)/sys/amd64/include/, specialreg.h fpu.h sigframe.h)


#
# ARM-specific headers
#
DIRS += include/spec/arm/libc
DIR_CONTENT(include/spec/arm/libc) := \
    $(call common_include_libc_arch_content,arm,arm) \
    $(D)/lib/msun/arm/fenv.h

DIRS += include/spec/arm/libc/machine
DIR_CONTENT(include/spec/arm/libc/machine) := \
    $(call common_include_libc_arch_machine_content,arm) \
    $(addprefix $(D)/sys/arm/include/, armreg.h atomic-v6.h ieee.h sysreg.h sigframe.h)


#
# ARM64-specific headers
#
DIRS += include/spec/arm_64/libc
DIR_CONTENT(include/spec/arm_64/libc) := \
    $(call common_include_libc_arch_content,arm64,aarch64) \
    $(D)/lib/msun/aarch64/fenv.h

DIRS += include/spec/arm_64/libc/machine
DIR_CONTENT(include/spec/arm_64/libc/machine) := \
    $(call common_include_libc_arch_machine_content,arm64) \
    $(addprefix $(D)/sys/arm64/include/, armreg.h)


#
# Rules for generating files
#

$(D)/include/rpcsvc/nis_object.x : $(DOWNLOADS)

%.h: %.x
	@$(MSG_GENERATE)$(notdir $@)
	$(VERBOSE)rm -f $@
	$(VERBOSE)rpcgen -C -h -DWANT_NFS3 $< -o $@

generated_rpcsvc_files := \
    $(D)/include/rpc/rpcb_prot.h \
    $(addprefix $(D)/include/rpcsvc/,\
                nis.h)

$(generated_rpcsvc_files:.h=.x): $(DOWNLOADS)

$(generated_rpcsvc_files) : $(generated_rpcsvc_files:.h=.x)

generated_files += $(generated_rpcsvc_files)

# generate files before assembling the public include directory tree
_dirs: $(generated_files)
$(generated_files) : $(DOWNLOADS)

default: include/libc/osreldate.h
include/libc/osreldate.h: _dirs
	@$(MSG_GENERATE)$(notdir $@)
	$(VERBOSE)echo '#include <sys/param.h>' > $@


#
# Create symlinks for sys headers expected in parent directory
#

default: create_symlinks
create_symlinks: _dirs
	@$(MSG_PREFIX)create symlinks
	$(VERBOSE)ln -sf sys/poll.h include/libc/poll.h
