Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Building with Bazel  (Read 2392 times)

0 Members and 1 Guest are viewing this topic.

jeez

  • Newbie
  • *
  • Posts: 1
    • View Profile
Building with Bazel
« on: August 08, 2020, 11:03:53 pm »
Does anybody have experience with bazel projects that use SFML? I'm new to both.
I added this to my bazel WORKSPACE


workspace(name = "myfirstsfml")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "sfml",
    urls = [
        "https://www.sfml-dev.org/files/SFML-2.5.1-macOS-clang.tar.gz",
    ],
    sha256 = "6af0f14fbd41dc038a00d7709f26fb66bb7ccdfe6187657ef0ef8cba578dcf14",
    build_file = "BUILD.sfml",
)


And this is my external/BUILD.sfml

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

cc_library(
    name = "sfml",
    hdrs = glob([
        "SFML-2.5.1-macos-clang/include/**/*.hpp",
        "SFML-2.5.1-macos-clang/include/**/*.inl",
    ]),
    copts = [
        "-Iexternal/SFML-2.5.1-macos-clang/include",
    ],
    visibility = ["//visibility:public"],
)


Getting a bunch of undefined symbols now, such as

Undefined symbols for architecture x86_64:
  "sf::RenderTarget::clear(sf::Color const&)", referenced from:
      _main in main.o

Tojot

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Building with Bazel (on macOS)
« Reply #1 on: November 06, 2022, 06:12:40 pm »
I'm starting with SFML and I insist to use it with Bazel on macOS Monterey.
I'm not an expert on Bazel or macOS specific development, and I didn't manage to get a fully hermetic setup, but I got a few steps closer.

The setup is not perfect, but I didn't find any better one on the Internet, so I've decided to share it here.
My goal is to have some fun with SFML, and this approach works of me for now, but if you manage to improve it, I'm happy to try it out.

What works:
  • Once installed, you can build and run SFML based applications with Bazel.
  • This works on macOS.

What could work better:
  • I did no attempt to support other systems.
  • I did no attempt to build SFML from source.
  • The application is a command line application, not an application bundle, and so it is not distribution ready.
  • The frameworks from extlibs/ have to be manually downloaded, unquarantined, and copied to "/Library/Frameworks/"

I'm using the dylib distribution of SFML, because Bazel is able to easily make them available to the binary through a simple srcs = glob(["lib/*"]) inclusion in a hermetic way.
I don't know if there is a simple way to include framework based distribution hermetically.

It is simple enough to use frameworks installed in the OS with brew (non-hermetic):

This is what I did for the SFML extlibs/. I'm not an expert in macOS application bundles, and I don't know how, and if at all it's possible to point SFML dylibs to frameworks located in custom directories, or easy or hard would it be to build them together as a cc_binary in Bazel, or finally how much work would it be to package it into an Application bundle, without Xcode doing all the heavy lifting.

Now, to the good stuff:

My WORKSPACE.bazel file:

workspace(name = "my workspace")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "sfml",
    build_file = "@//external/sfml:BUILD.bazel",
    sha256 = "6af0f14fbd41dc038a00d7709f26fb66bb7ccdfe6187657ef0ef8cba578dcf14",
    strip_prefix = "SFML-2.5.1-macos-clang",
    urls = ["https://www.sfml-dev.org/files/SFML-2.5.1-macOS-clang.tar.gz"],
)


My external/sfml:BUILD.bazel file

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

cc_library(
    name = "sfml",
    srcs = glob(["lib/*"]),
    hdrs = glob([
        "include/**/*.hpp",
        "include/**/*.inl",
    ]),
    # This is not a hermetic http_archive, because SFML depends on additional frameworks.
    # These had to be manually unquarantined and copied to "/Library/Frameworks/"
    # % cd /Users/tojot/Downloads/SFML-2.5.1-macos-clang/extlibs
    # % xattr *
    # % xattr -rd com.apple.quarantine  *
    # % xattr *
    copts = [
        "-framework libogg",
        "-framework flac",
        "-framework libpng",
        "-framework freetype",
        "-framework vorbisenc",
    ],
    # Include libraries like this: #include "SFML/Graphics.hpp"
    strip_include_prefix = "include/",
    visibility = ["//visibility:public"],
)

« Last Edit: November 06, 2022, 08:16:21 pm by tojot »

 

anything