# Copyright 2026 Open Source Robotics Foundation, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. load("@cmake_configure_file//:cmake_configure_file.bzl", "cmake_configure_file") load("@rules_cc//cc:cc_library.bzl", "cc_library") load("@rules_cc//cc:cc_shared_library.bzl", "cc_shared_library") config_setting( name = "windows_x86_32", constraint_values = [ "@platforms//os:windows", "@platforms//cpu:x86_32", ], ) config_setting( name = "windows_x86_64", constraint_values = [ "@platforms//os:windows", "@platforms//cpu:x86_64", ], ) MIMICK_DEFINES_CPU = select({ ":windows_x86_64": [ "MMK_ARCH_x86=1", "MMK_ARCH=x86_64", "MMK_BITS=64", ], "@platforms//cpu:x86_64": [ "MMK_ARCH_x86_64=1", "MMK_ARCH=x86_64", "MMK_BITS=64", ], "@platforms//cpu:x86_32": [ "MMK_ARCH_x86=1", "MMK_ARCH=i386", "MMK_BITS=32", ], "@platforms//cpu:aarch64": [ "MMK_ARCH_ARM64=1", "MMK_ARCH=aarch64", "MMK_BITS=64", ], "@platforms//cpu:aarch32": [ "MMK_ARCH_ARM=1", "MMK_ARCH=arm", "MMK_BITS=32", ], }) MIMICK_DEFINES_OS = select({ "windows_x86_32": [ "MMK_EXE_FMT_PE=1", "MMK_MANGLING=leading-underscore", "HAVE___STDIO_COMMON_VFPRINTF=1", ], "@platforms//os:windows": [ "MMK_EXE_FMT_PE=1", "HAVE___STDIO_COMMON_VFPRINTF=1", ], "@platforms//os:macos": [ "MMK_EXE_FMT_MACH_O=1", "HAVE_MMAP=1", "HAVE_MMAP_MAP_ANONYMOUS=1", "HAVE_MMAP_MAP_ANON=1", "MMK_MANGLING=leading-underscore", ], "@platforms//os:freebsd": [ "MMK_EXE_FMT_ELF=1", "HAVE_ELF_AUXINFO=1", "HAVE__R_DEBUG=1", "HAVE__DYNAMIC=1", "HAVE_MMAP=1", "HAVE_MMAP_MAP_ANONYMOUS=1", "HAVE_MMAP_MAP_ANON=1", "MMK_MANGLING=none", ], "@platforms//os:linux": [ "MMK_EXE_FMT_ELF=1", "HAVE_ELF_AUXV_T=1", "HAVE__R_DEBUG=1", "HAVE__DYNAMIC=1", "HAVE_MMAP=1", "HAVE_MMAP_MAP_ANONYMOUS=1", "HAVE_MMAP_MAP_ANON=1", "MMK_MANGLING=none", ], }) PLATFORM_SPECIFIC_PLT_SRCS = select({ "@platforms//os:windows": [ "src/plt-pe.h", "src/plt-pe.c", ], "@platforms//os:macos": [ "src/plt-mach-o.h", "src/plt-mach-o.c", ], "//conditions:default": [ "src/plt-elf.h", "src/plt-elf.c", ], }) PLATFORM_SPECIFIC_TRAMPOLINE_SRCS = select({ ":windows_x86_64": ["src/asm/trampoline-x86_64-win.asm"], "@platforms//cpu:x86_64": ["src/asm/trampoline-x86_64-systemv.S"], "@platforms//cpu:x86_32": ["src/asm/trampoline-i386-cdecl.S"], "@platforms//cpu:aarch32": ["src/asm/trampoline-arm.S"], "@platforms//cpu:aarch64": ["src/asm/trampoline-aarch64.S"], "//conditions:default": [], }) PLATFORM_AGNOSTIC_SRCS = glob( [ "src/**/*.h", "src/**/*.c", ], exclude = ["src/plt-*"], ) # Auto-generate config.h cmake_configure_file( name = "config_h", src = "src/config.h.in", out = "src/config.h", defines = MIMICK_DEFINES_OS + MIMICK_DEFINES_CPU, visibility = ["//visibility:private"], ) # Library cc_library( name = "mimick", srcs = [":config_h"] + PLATFORM_AGNOSTIC_SRCS + PLATFORM_SPECIFIC_TRAMPOLINE_SRCS + PLATFORM_SPECIFIC_PLT_SRCS, hdrs = glob(["include/**/*.h"]), includes = [ "include", "src", ], linkopts = select({ "@platforms//os:linux": ["-lrt"], "//conditions:default": [], }), local_defines = select({ "@platforms//os:windows": [ "_CRT_SECURE_NO_WARNINGS", "WIN32_LEAN_AND_MEAN", ], "//conditions:default": [ "_GNU_SOURCE", ], }), visibility = ["//visibility:public"], ) cc_shared_library( name = "mimick_dynamic", exports_filter = [":mimick"], features = ["windows_export_all_symbols"], visibility = ["//visibility:public"], deps = [":mimick"], )