Skip to content

Conversation

@mercury233
Copy link
Contributor

What does this PR do?

Let os.findheader can support searching relative paths.
Some libraries, by default, install header files within folders. For example, the header files for libfreetype-dev are located in:
/usr/include/freetype2/freetype/*.h
/usr/include/freetype2/ft2build.h

Previously, if a program included the header with #include <ft2build.h>, you had to use:
os.findheader("freetype2/ft2build.h") .. "freetype2"
to locate it.

Now you can simply use:
os.findheader("ft2build.h", "freetype2")
to achieve the same result.

How does this PR change Premake's behavior?

Nothing.

Anything else we should know?

Nothing.

Did you check all the boxes?

  • Focus on a single fix or feature; remove any unrelated formatting or code changes
  • Add unit tests showing fix or feature works; all tests pass
  • Mention any related issues (put closes #XXXX in comment to auto-close issue when PR is merged)
  • Follow our coding conventions
  • Minimize the number of commits
  • Align documentation to your changes

You can now support Premake on our OpenCollective. Your contributions help us spend more time responding to requests like these!

@mercury233 mercury233 changed the title Patch findheader os.findheader support relative headerdirs May 17, 2025
`headerfile` is a file name of a file path to locate.

`additionalpaths` is a string or a table of one or more additional search path.
`additionalpaths` is a string or a table of one or more additional search path. Can be absolute or relative paths. If relative, they are relative to all the default search paths.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: "The paths may be absolute or relative. If the path is a relative path, it is relative to each of the default search paths."


for _, userpath in ipairs(userpaths) do
if path.isabsolute(userpath) then
paths = table.join({userpath}, paths)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "append" should be use instead of "prepend", to keep original order

paths = table.join({userpath}, paths)
else
for _, p in ipairs(paths) do
paths = table.join({path.join(p, userpath)}, paths)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the search should prioritize subdirectories. For example, if a user's computer has both /usr/include/lua.h and /usr/include/lua5.1/lua.h, and the user calls os.findheader("lua.h", "lua5.1"), they are more likely looking for the latter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition you iterate and modify paths, so os.findheader('lib.h', {'dir1', dir2}) will result in something like
{'/usr/dir1/dir2', '/usr/include/dir2', '/usr/dir1', '/usr/include'}

A table.transform(userpaths to transform relative paths into a table, and keep absolute path as-is would be better, then flatten userpaths, and finally paths = table.join(userpaths, paths)

@Jarod42
Copy link
Contributor

Jarod42 commented May 23, 2025

How does this PR change Premake's behavior?

Nothing.

In fact behavior changes

os.findheader("header.h", "./submodules/library")

would no longer search from local directory.

Maybe creating another API would be cleaner, something like

function os.findheaderleaf(name, userpaths)
  local res = os.findheaderleaf(name, userpaths)
  if res then
    path.join(res, path.getdirectory(name))
  else
    return nil
  end
end

@mercury233 mercury233 marked this pull request as draft May 23, 2025 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants