bazel-central-registry icon indicating copy to clipboard operation
bazel-central-registry copied to clipboard

wanted: glfw/glfw

Open phaedon opened this issue 1 year ago • 3 comments
trafficstars

Module location

https://github.com/glfw/glfw

Link to bzlmod issue in the module's repository

No response

Any other context to provide?

For starters, here's a WORKSPACE rule I wrote to be able to use this in the meantime:

http_archive(
    name = "glfw-3.3.6",
    build_file_content = """
cc_library(
    name = "glfw-headers",
    hdrs = [
        "include/GLFW/glfw3.h",
    ],
    strip_include_prefix = "include",
    includes = ['.'],
)
cc_library(
    name = "glfw-3.3.6",
    srcs = [
        "src/context.c",
        "src/egl_context.c",
        "src/glx_context.c",
        "src/init.c",
        "src/input.c",
        "src/linux_joystick.c",
        "src/monitor.c",
        "src/osmesa_context.c",
        "src/posix_thread.c",
        "src/posix_time.c",
        "src/vulkan.c",
        "src/window.c",
        "src/x11_init.c",
        "src/x11_monitor.c",
        "src/x11_window.c",
        "src/xkb_unicode.c",
    ],
    hdrs = [
        "include/GLFW/glfw3.h",
        "include/GLFW/glfw3native.h",
        "src/egl_context.h",
        "src/glx_context.h",
        "src/internal.h",
        "src/linux_joystick.h",
        "src/mappings.h",
        "src/osmesa_context.h",
        "src/posix_thread.h",
        "src/posix_time.h",
        "src/x11_platform.h",
        "src/xkb_unicode.h",
    ],
    defines = ["_GLFW_X11",],
    linkopts = ["-lX11",],
)
""",
    sha256 = "ed07b90e334dcd39903e6288d90fa1ae0cf2d2119fec516cf743a0a404527c02",
    strip_prefix = "glfw-3.3.6",
    urls = ["https://github.com/glfw/glfw/archive/refs/tags/3.3.6.tar.gz"],
)

Fund our work

  • [ ] Sponsor our community's open source work by donating a feature bounty

phaedon avatar Jan 10 '24 14:01 phaedon

@Vertexwahn

phaedon avatar Feb 01 '24 15:02 phaedon

@phaedon

I use GLFW this way in my WORKSPACE.bazel file:

#-------------------------------------------------------------
# GLFW
#-------------------------------------------------------------

GLFW_VERSION = "3.3.8"

http_archive(
    name = "glfw",
    build_file = "//bazel:glfw.BUILD",
    sha256 = "f30f42e05f11e5fc62483e513b0488d5bceeab7d9c5da0ffe2252ad81816c713",
    strip_prefix = "glfw-{}".format(GLFW_VERSION),
    urls = ["https://github.com/glfw/glfw/archive/{}.tar.gz".format(GLFW_VERSION)],
)

new_local_repository(
    name = "system_libs",
    build_file_content = """cc_library(
    name = "x11",
    srcs = ["libX11.so"],
    visibility = ["//visibility:public"],
)
""",
    # pkg-config --variable=libdir x11
    path = "/usr/lib/x86_64-linux-gnu",
)

glfw.BUILD looks like this:

load("@rules_cc//cc:defs.bzl", "cc_library", "objc_library")

# ===== win32 =====

WIN32_DEFINES = [
    "_GLFW_WIN32",
    "GLFW_INVALID_CODEPOINT",
]

WIN32_HDRS = [
    "src/win32_joystick.h",
    "src/win32_platform.h",
    "src/wgl_context.h",
]

WIN32_SRCS = [
    "src/win32_init.c",
    "src/win32_joystick.c",
    "src/win32_monitor.c",
    "src/win32_thread.c",
    "src/win32_time.c",
    "src/win32_window.c",
    "src/wgl_context.c",
]

WIN32_LINKOPTS = [
    "-DEFAULTLIB:user32.lib",
    "-DEFAULTLIB:gdi32.lib",
    "-DEFAULTLIB:shell32.lib",
]

# ===== linux =====

LINUX_DEFINES = [
    "_GLFW_HAS_XF86VM",
    "_GLFW_X11",
]

LINUX_HDRS = [
    "src/glx_context.h",
    "src/linux_joystick.h",
    "src/posix_thread.h",
    "src/posix_time.h",
    "src/x11_platform.h",
]

LINUX_SRCS = [
    "src/glx_context.c",
    "src/linux_joystick.c",
    "src/posix_thread.c",
    "src/posix_time.c",
    "src/x11_init.c",
    "src/x11_monitor.c",
    "src/x11_window.c",
]

LINUX_LINKOPTS = []

# ===== darwin =====

DARWIN_DEFINES = [
    "_GLFW_COCOA",
    "_GLFW_NSGL",
    "_GLFW_NO_DLOAD_WINMM",
    "_GLFW_USE_OPENGL",
]

DARWIN_HDRS = [
    "src/cocoa_joystick.h",
    "src/cocoa_platform.h",
    "src/glx_context.h",
    "src/nsgl_context.h",
    "src/null_joystick.h",
    "src/null_platform.h",
    "src/posix_thread.h",
    "src/wl_platform.h",
]

DARWIN_SRCS = [
    "src/cocoa_time.c",
    "src/posix_thread.c",
    "src/cocoa_init.m",
    "src/cocoa_joystick.m",
    "src/cocoa_monitor.m",
    "src/cocoa_window.m",
    "src/nsgl_context.m",
]

DARWIN_LINKOPTS = [
    "-framework OpenGL",
    "-framework Cocoa",
    "-framework IOKit",
    "-framework CoreFoundation",
]

# ===== common =====

COMMON_HDRS = [
    "include/GLFW/glfw3.h",
    "include/GLFW/glfw3native.h",
    "src/egl_context.h",
    "src/internal.h",
    "src/osmesa_context.h",
    "src/mappings.h",
    "src/xkb_unicode.h",
]

COMMON_SRCS = [
    "src/context.c",
    "src/egl_context.c",
    "src/init.c",
    "src/input.c",
    "src/osmesa_context.c",
    "src/monitor.c",
    "src/vulkan.c",
    "src/window.c",
    "src/xkb_unicode.c",
]

cc_library(
    name = "glfw_src",
    srcs = COMMON_SRCS + select({
        "@bazel_tools//src/conditions:windows": WIN32_SRCS,
        "@bazel_tools//src/conditions:linux_x86_64": LINUX_SRCS,
    }),
    hdrs = COMMON_HDRS + select({
        "@bazel_tools//src/conditions:windows": WIN32_HDRS,
        "@bazel_tools//src/conditions:linux_x86_64": LINUX_HDRS,
    }),
    defines = select({
        "@bazel_tools//src/conditions:windows": WIN32_DEFINES,
        "@bazel_tools//src/conditions:linux_x86_64": LINUX_DEFINES,
    }),
)

objc_library(
    name = "glfw_src_darwin",
    srcs = COMMON_SRCS + DARWIN_SRCS,
    hdrs = COMMON_HDRS + DARWIN_HDRS,
    copts = ["-fno-objc-arc"],
    defines = DARWIN_DEFINES + ["GLFW_INVALID_CODEPOINT"],
)

cc_library(
    name = "glfw",
    hdrs = [
        "include/GLFW/glfw3.h",
        "include/GLFW/glfw3native.h",
    ],
    linkopts = select({
        "@bazel_tools//src/conditions:windows": WIN32_LINKOPTS,
        "@bazel_tools//src/conditions:linux_x86_64": LINUX_LINKOPTS,
        "@bazel_tools//src/conditions:darwin": DARWIN_LINKOPTS,
    }),
    strip_include_prefix = "include",
    visibility = ["//visibility:public"],
    deps = select({
        "@bazel_tools//src/conditions:windows": [":glfw_src"],
        "@bazel_tools//src/conditions:linux_x86_64": [":glfw_src"],
        "@bazel_tools//src/conditions:darwin": [":glfw_src_darwin"],
    }),
)

Works on macOS, Windows and Ubuntu 22.04.

The source for this was https://github.com/Morfly/vulkan-bazel-samples/blob/main/third_party/glfw/glfw.BUILD Available under MIT License:


MIT License

Copyright (c) 2021 Pavlo Stavytskyi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

I did a few minor modifications - my modifications also available under the same License

Vertexwahn avatar Feb 02 '24 23:02 Vertexwahn