facebook / buck2

Build system, successor to Buck
https://buck2.build/
Apache License 2.0
3.33k stars 194 forks source link

Added support for headers in the windows_resource rule #607

Closed Overhatted closed 1 month ago

Overhatted commented 2 months ago

Like other cxx rules it supports specifying "headers" or "include_directories" and "raw_headers".

As a test, I added a header dependency to the test I provided in the previous PR: #581.

The final result being the following files: BUCK:

# A rule that includes a single .rc file and compiles it into an object file.
windows_resource(
    name = "resources",
    srcs = [
        "resources.rc",
    ],
    header_namespace = "",
    headers = ["message.h"],
)

# A rule that links against the above windows_resource rule.
# A rule that includes a single .rc file and compiles it into an object file.
windows_resource(
    name = "resources",
    srcs = [
        "resources.rc",
    ],
    header_namespace = "",
    headers = ["message.h"],
)

# A rule that links against the above windows_resource rule.
cxx_binary(
    name = "app",
    srcs = [
        "main.cpp",
    ],
    deps = [
        ":resources"
    ],
    linker_flags = [
        "User32.lib",
    ],
)

main.cpp:

#include <iostream>
#include <Windows.h>

int main(int argc, const char* argv[])
{
    std::string message;
    message.resize(50);
    int returnValue = LoadStringA(NULL, 4, message.data(), message.size());
    if (returnValue == 0)
    {
        std::cout << "Failed to read resource";
        return 1;
    }

    message.resize(returnValue);
    std::cout << message;
    return 0;
}

message.h:

#define MESSAGE_TO_PRINT "Hello from header"

resources.rc:

#include <winresrc.h>

#include "message.h"

STRINGTABLE
BEGIN
4 MESSAGE_TO_PRINT
END
Overhatted commented 1 month ago

Hello @KapJI, you reviewed my last Windows PR #581, can you take a look at this one?

facebook-github-bot commented 1 month ago

@KapJI has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

KapJI commented 1 month ago

Looks good to me, thanks!