Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/base/os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,16 @@
elseif type(headerdirs) == "table" then
userpaths = headerdirs
end
paths = table.join(userpaths, 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

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)

end
end
end

local result = os.pathsearch (headerpath, table.unpack(paths))
return result
Expand Down
9 changes: 9 additions & 0 deletions tests/base/test_os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@
os.findheader("test.h", path.getabsolute("folder/subfolder/include")))
end

function suite.findheader_provided_relative()
local os_getenv = os.getenv
os.getenv = create_mock_os_getenv({ [get_LD_PATH_variable_name()] = get_surrounded_env_path("folder/subfolder/lib") })

test.isequal(path.getabsolute("folder/subfolder/include/testlib"), os.findheader("testlib2.h", "testlib"))

os.getenv = os_getenv
end

function suite.findheader_frompath_lib()
local os_getenv = os.getenv
os.getenv = create_mock_os_getenv({ [get_LD_PATH_variable_name()] = get_surrounded_env_path("folder/subfolder/lib") })
Expand Down
1 change: 1 addition & 0 deletions tests/folder/subfolder/include/testlib/testlib2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Only used for presence in tests
11 changes: 9 additions & 2 deletions website/docs/os/os.findheader.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ p = os.findheader("headerfile" [, additionalpaths])

### Parameters ###

`headerfile` is a file name or a the end of a file path to locate.
`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. The paths may be absolute or relative. If the path is a relative path, it is relative to each of the default search paths.

### Return Value ###

The path containing the header file, if found. Otherwise, nil.

### Example ###

``` lua
os.findheader("event.h") -- /usr/include
os.findheader("ft2build.h", "freetype2") -- /usr/include/freetype2
```

### Remarks ###
`os.findheader` mostly use the same paths as [[os.findlib]] but replace `/lib` by `/include`.

Expand Down