load("@bazel_skylib//rules:copy_file.bzl", "copy_file") load("@rules_cc//cc:defs.bzl", "cc_library") load( "//:config_linux.bzl", LINUX_AUTOMAKE_VARS = "AUTOMAKE_VARIABLES", LINUX_DELETE_VARS = "DELETE_VARIABLES", LINUX_DIRECT_VARS = "DIRECT_VARIABLES", LINUX_INLINE_VARS = "INLINE_VARIABLES", ) load( "//:config_macos.bzl", MACOS_AUTOMAKE_VARS = "AUTOMAKE_VARIABLES", MACOS_DELETE_VARS = "DELETE_VARIABLES", MACOS_DIRECT_VARS = "DIRECT_VARIABLES", MACOS_INLINE_VARS = "INLINE_VARIABLES", ) load("//:utils.bzl", "sed_command") COPTS = [ "-w", ] libdiffutils_a_SOURCES = [ "allocator.c", "argmatch.c", "openat-proc.c", "basename-lgpl.c", "binary-io.c", "bitrotate.c", "btoc32.c", "c-ctype.c", "c-file-type.c", "c-stack.c", "c-strcasecmp.c", "c32_apply_type_test.c", "c32_get_type_test.c", "c32isalnum.c", "c32isalpha.c", "c32isblank.c", "c32iscntrl.c", "c32isdigit.c", "c32isgraph.c", "c32islower.c", "c32isprint.c", "c32ispunct.c", "c32isspace.c", "c32isupper.c", "c32isxdigit.c", "c32tolower.c", "c32width.c", "careadlinkat.c", "cloexec.c", "dirname.c", "basename.c", "dirname-lgpl.c", "stripslash.c", "exclude.c", "exitfail.c", "fd-hook.c", "filenamecat.c", "filenamecat-lgpl.c", "gettime.c", "malloc/dynarray_at_failure.c", "malloc/dynarray_emplace_enlarge.c", "malloc/dynarray_finalize.c", "malloc/dynarray_resize.c", "malloc/dynarray_resize_clear.c", "hard-locale.c", "hash.c", "ialloc.c", "localcharset.c", "glthread/lock.c", "malloca.c", "mbscasecmp.c", "mbsrtoc32s.c", "mbszero.c", "mcel.c", "nstrftime.c", "glthread/once.c", "openat-die.c", "progname.c", "propername-lite.c", "quotearg.c", "same-inode.c", "save-cwd.c", "setlocale_null.c", "setlocale_null-unlocked.c", "sh-quote.c", "sig-handler.c", "stat-time.c", "stdlib.c", "stdopen.c", "strnlen1.c", "system-quote.c", "tempname.c", "glthread/threadlib.c", "timespec.c", "unistd.c", "version-etc.c", "version-etc-fsf.c", "wctype-h.c", "xmalloc.c", "xalloc-die.c", "xfreopen.c", "xmalloca.c", "xstdopen.c", "xstrtoimax.c", "xstrtol.c", "xstrtoul.c", "cmpbuf.c", "diagnose.c", ] + select({ "@platforms//os:macos": [ "chdir-long.c", "error.c", "free.c", "fstat.c", "fstatat.c", "getopt.c", "getopt1.c", "getrandom.c", "lstat.c", "mbrtoc32.c", "mempcpy.c", "memrchr.c", "nl_langinfo.c", "open.c", "openat.c", "rawmemchr.c", "readlink.c", "readlinkat.c", "reallocarray.c", "regex.c", "sigsegv.c", "stackvma.c", "stat.c", "stdbit.c", "stdc_bit_width.c", "stdc_leading_zeros.c", "strerror.c", "strerror-override.c", "strtoll.c", "time_rz.c", "timegm.c", "unicase/tolower.c", "unictype/ctype_alnum.c", "unictype/ctype_alpha.c", "unictype/ctype_blank.c", "unictype/ctype_cntrl.c", "unictype/ctype_digit.c", "unictype/ctype_graph.c", "unictype/ctype_lower.c", "unictype/ctype_print.c", "unictype/ctype_punct.c", "unictype/ctype_space.c", "unictype/ctype_upper.c", "unictype/ctype_xdigit.c", "unistr/u32-chr.c", "unistr/u32-cpy.c", "unistr/u32-pcpy.c", "unistr/u32-strcat.c", "unistr/u32-strlen.c", "uniwidth/width.c", "wcwidth.c", "wmempcpy.c", ], "@platforms//os:linux": [ "btowc.c", "chdir-long.c", "fcntl.c", "getprogname.c", "mbrtoc32.c", "mbrtowc.c", "mbsrtowcs.c", "nl_langinfo.c", "realloc.c", "reallocarray.c", "sigsegv.c", "stackvma.c", "stdbit.c", "stdc_bit_width.c", "stdc_leading_zeros.c", "time_rz.c", "unicase/tolower.c", "unictype/ctype_alnum.c", "unictype/ctype_alpha.c", "unictype/ctype_blank.c", "unictype/ctype_cntrl.c", "unictype/ctype_digit.c", "unictype/ctype_graph.c", "unictype/ctype_lower.c", "unictype/ctype_print.c", "unictype/ctype_punct.c", "unictype/ctype_space.c", "unictype/ctype_upper.c", "unictype/ctype_xdigit.c", "unistr/u32-chr.c", "unistr/u32-cpy.c", "unistr/u32-pcpy.c", "unistr/u32-strcat.c", "unistr/u32-strlen.c", "uniwidth/width.c", ], }) BUILT_SOURCES = [ "ctype.h", "dirent.h", "error.h", "fcntl.h", "malloc/dynarray.gl.h", "malloc/dynarray-skeleton.gl.h", "inttypes.h", "langinfo.h", "locale.h", "pthread.h", "sched.h", "signal.h", "stdio.h", "stdlib.h", "string.h", "strings.h", "sys/random.h", "sys/stat.h", "sys/time.h", "sys/types.h", "sys/wait.h", "time.h", "uchar.h", "unistd.h", "wchar.h", "wctype.h", ] + select({ "@platforms//os:macos": [ "alloca.h", "assert.h", "fnmatch.h", "getopt.h", "getopt-cdefs.h", "limits.h", "sigsegv.h", "stdbit.h", "stdckdint.h", "stddef.h", "stdint.h", "unicase.h", "unictype.h", "uninorm.h", "unistr.h", "unitypes.h", "uniwidth.h", ], "@platforms//os:linux": [ "alloca.h", "limits.h", "sigsegv.h", "stdbit.h", "stdckdint.h", "stddef.h", "unicase.h", "unictype.h", "uninorm.h", "unistr.h", "unitypes.h", "uniwidth.h", ], }) HEADER_TEMPLATES = { "alloca.h": "alloca.in.h", "assert.almost.in.h": "assert.in.h", # special case requiring two passes, see :assert_h below "ctype.h": "ctype.in.h", "dirent.h": "dirent.in.h", "errno.h": "errno.in.h", "error.h": "error.in.h", "fcntl.h": "fcntl.in.h", "fnmatch.h": "fnmatch.in.h", "getopt.h": "getopt.in.h", "getopt-cdefs.h": "getopt-cdefs.in.h", "malloc/dynarray.gl.h": "malloc/dynarray.h", "malloc/dynarray-skeleton.gl.h": "malloc/dynarray-skeleton.c", "inttypes.h": "inttypes.in.h", "langinfo.h": "langinfo.in.h", "limits.h": "limits.in.h", "locale.h": "locale.in.h", "pthread.h": "pthread.in.h", "sched.h": "sched.in.h", "signal.h": "signal.in.h", "sigsegv.h": "sigsegv.in.h", "stdarg.h": "stdarg.in.h", "stdbit.h": "stdbit.in.h", "stdckdint.h": "stdckdint.in.h", "stddef.h": "stddef.in.h", "stdint.h": "stdint.in.h", "stdio.h": "stdio.in.h", "stdlib.h": "stdlib.in.h", "string.h": "string.in.h", "strings.h": "strings.in.h", "sys/random.h": "sys_random.in.h", "sys/stat.h": "sys_stat.in.h", "sys/time.h": "sys_time.in.h", "sys/types.h": "sys_types.in.h", "sys/wait.h": "sys_wait.in.h", "time.h": "time.in.h", "uchar.h": "uchar.in.h", "unicase.h": "unicase.in.h", "unictype.h": "unictype.in.h", "uninorm.h": "uninorm.in.h", "unistd.h": "unistd.in.h", "unistr.h": "unistr.in.h", "unitypes.h": "unitypes.in.h", "uniwidth.h": "uniwidth.in.h", "wchar.h": "wchar.in.h", "wctype.h": "wctype.in.h", } USES_DIRECT_REPLACEMENTS = [ "malloc/dynarray-skeleton.gl.h", ] [ genrule( name = hdr[:-len(".h")] + "_h", srcs = [template] + select({ "@platforms//os:macos": MACOS_INLINE_VARS.values(), "@platforms//os:linux": LINUX_INLINE_VARS.values(), }), outs = [hdr], cmd = select({ "@platforms//os:osx": sed_command( automake_vars = MACOS_AUTOMAKE_VARS, delete_vars = MACOS_DELETE_VARS, direct_vars = MACOS_DIRECT_VARS, inline_vars = MACOS_INLINE_VARS, sed = "@sed", template = template, use_direct_vars = True if hdr in USES_DIRECT_REPLACEMENTS else False, ), "@platforms//os:linux": sed_command( automake_vars = LINUX_AUTOMAKE_VARS, delete_vars = LINUX_DELETE_VARS, direct_vars = LINUX_DIRECT_VARS, inline_vars = LINUX_INLINE_VARS, sed = "@sed", template = template, use_direct_vars = True if hdr in USES_DIRECT_REPLACEMENTS else False, ), }), tags = ["manual"], tools = ["@sed"], ) for hdr, template in HEADER_TEMPLATES.items() ] # Perform a second sed pass on the assert.in.h template after verify.h # has been inlined in order to substitute variables into the inlined # content and to remove a chunk. This is necessary because the inlined # content will not be in sed's pattern buffer on the first pass. Since # this is the only template that needs two passes, treat it as a special # case here rather than generalizing. REPLACE_BETWEEN = "-e '/@assert.h omit start@/,/@assert.h omit end@/d'" genrule( name = "assert_h", srcs = ["assert.almost.in.h"] + MACOS_INLINE_VARS.values(), outs = ["assert.h"], cmd = sed_command( automake_vars = MACOS_AUTOMAKE_VARS, delete_vars = MACOS_DELETE_VARS, direct_vars = MACOS_DIRECT_VARS, generated_header = False, inline_vars = {}, sed = "@sed", template = "assert.almost.in.h", use_direct_vars = True, ).replace( "> $@", " {} > $@".format(REPLACE_BETWEEN), ), tags = ["manual"], tools = ["@sed"], ) cc_library( name = "diffutils", srcs = libdiffutils_a_SOURCES + glob( ["**/*.h"], exclude = ["**/*.in.h"], ) + BUILT_SOURCES + select({ "@platforms//os:macos": [ # TODO: unclear why the following are needed to avoid # linking errors on osx. mktime.c should be linked in # via the dep on :diffutils_a-mktime. "mktime.c", "setlocale-lock.c", ], "@platforms//os:linux": [], }), copts = COPTS + [ "-DHAVE_CONFIG_H", ], includes = ["."], textual_hdrs = [ "xstrtol.c", "strftime.c", ] + select({ "@platforms//os:macos": [ "regex_internal.c", "strtol.c", "regcomp.c", "regexec.c", "malloc/dynarray-skeleton.c", ], "@platforms//os:linux": [], }), visibility = ["//src:__subpackages__"], deps = [ ":diffutils_a-fopen", ":diffutils_a-mbsrtoc32s-state", ":diffutils_a-mktime", ] + select({ "@platforms//os:macos": [ ":diffutils_a-fnmatch", ], "@platforms//os:linux": [ ":diffutils_a-mbsrtowcs-state", ], }), ) cc_library( name = "diffutils_a-fnmatch", srcs = [ "attribute.h", "cdefs.h", "config.h", "flexmember.h", "fnmatch.c", "fnmatch.h", "idx.h", "intprops-internal.h", "libc-config.h", "stdckdint.h", "string.h", "uchar.h", "wchar.h", ], copts = COPTS, includes = ["."], tags = [ "manual", ], textual_hdrs = ["fnmatch_loop.c"], ) cc_library( name = "diffutils_a-fopen", srcs = [ "config.h", "fcntl.h", "fopen.c", "stdio.h", ], copts = COPTS, includes = ["."], ) cc_library( name = "diffutils_a-mbsrtoc32s-state", srcs = [ "config.h", "mbsrtoc32s-state.c", ], copts = COPTS, includes = ["."], ) cc_library( name = "diffutils_a-mbsrtowcs-state", srcs = [ "config.h", "mbsrtowcs-state.c", ], copts = COPTS, includes = ["."], tags = [ "manual", ], ) cc_library( name = "diffutils_a-mktime", srcs = [ "cdefs.h", "config.h", "intprops.h", "intprops-internal.h", "libc-config.h", "mktime.c", "mktime-internal.h", "stdckdint.h", ], copts = COPTS, includes = ["."], ) copy_file( name = "config_h", src = select({ ":is_linux_arm64": "config_linux_arm64.h", ":is_linux_amd64": "config_linux_amd64.h", ":is_macos_arm64": "config_macos_arm64.h", ":is_macos_amd64": "config_macos_amd64.h", }), out = "config.h", ) config_setting( name = "is_linux_arm64", constraint_values = [ "@platforms//os:linux", "@platforms//cpu:arm64", ], ) config_setting( name = "is_linux_amd64", constraint_values = [ "@platforms//os:linux", "@platforms//cpu:x86_64", ], ) config_setting( name = "is_macos_arm64", constraint_values = [ "@platforms//os:macos", "@platforms//cpu:arm64", ], ) config_setting( name = "is_macos_amd64", constraint_values = [ "@platforms//os:macos", "@platforms//cpu:x86_64", ], )