-
-
Notifications
You must be signed in to change notification settings - Fork 619
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Symbolic link support in Premake (#2352)
- Loading branch information
1 parent
3f4090a
commit 7f2a0c8
Showing
11 changed files
with
247 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* \file os_linkdir.c | ||
* \brief Creates a symbolic link to a directory. | ||
* \author Copyright (c) 2024 Jess Perkins and the Premake project | ||
*/ | ||
|
||
#include <sys/stat.h> | ||
#include "premake.h" | ||
|
||
int do_linkdir(lua_State* L, const char* src, const char* dst) | ||
{ | ||
#if PLATFORM_WINDOWS | ||
// Prepend the drive letter if a relative path is given | ||
char dstPath[MAX_PATH]; | ||
char srcPath[MAX_PATH]; | ||
|
||
do_normalize(L, srcPath, src); | ||
do_normalize(L, dstPath, dst); | ||
do_translate(dstPath, '\\'); | ||
do_translate(srcPath, '\\'); | ||
|
||
// Promote to wide path | ||
wchar_t wSrcPath[MAX_PATH]; | ||
wchar_t wDstPath[MAX_PATH]; | ||
|
||
MultiByteToWideChar(CP_UTF8, 0, srcPath, -1, wSrcPath, MAX_PATH); | ||
MultiByteToWideChar(CP_UTF8, 0, dstPath, -1, wDstPath, MAX_PATH); | ||
|
||
// If the source path is relative, prepend the current working directory | ||
if (!do_isabsolute(src)) | ||
{ | ||
// Get the current working directory | ||
wchar_t cwd[MAX_PATH]; | ||
GetCurrentDirectoryW(MAX_PATH, cwd); | ||
|
||
// Convert the source path to a relative path | ||
wchar_t relSrcPath[MAX_PATH]; | ||
swprintf(relSrcPath, MAX_PATH, L"%c:%s", cwd[0], wSrcPath); | ||
|
||
BOOLEAN res = CreateSymbolicLinkW(wDstPath, relSrcPath, SYMBOLIC_LINK_FLAG_DIRECTORY | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); | ||
return res != 0; | ||
} | ||
else | ||
{ | ||
BOOLEAN res = CreateSymbolicLinkW(wDstPath, wSrcPath, SYMBOLIC_LINK_FLAG_DIRECTORY | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); | ||
return res != 0; | ||
} | ||
#else | ||
int res = symlink(src, dst); | ||
return res == 0; | ||
#endif | ||
} | ||
|
||
int os_linkdir(lua_State* L) | ||
{ | ||
const char* src = luaL_checkstring(L, 1); | ||
const char* dst = luaL_checkstring(L, 2); | ||
|
||
int result = do_linkdir(L, src, dst); | ||
if (!result) | ||
{ | ||
lua_pushnil(L); | ||
lua_pushfstring(L, "Unable to create link from '%s' to '%s'", src, dst); | ||
return 2; | ||
} | ||
|
||
lua_pushboolean(L, 1); | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* \file os_linkfile.c | ||
* \brief Creates a symbolic link to a file. | ||
* \author Copyright (c) 2024 Jess Perkins and the Premake project | ||
*/ | ||
|
||
#include <sys/stat.h> | ||
#include "premake.h" | ||
|
||
int do_linkfile(lua_State* L, const char* src, const char* dst) | ||
{ | ||
#if PLATFORM_WINDOWS | ||
char dstPath[MAX_PATH]; | ||
char srcPath[MAX_PATH]; | ||
|
||
do_normalize(L, srcPath, src); | ||
do_normalize(L, dstPath, dst); | ||
do_translate(dstPath, '\\'); | ||
do_translate(srcPath, '\\'); | ||
|
||
// Promote to wide path | ||
wchar_t wSrcPath[MAX_PATH]; | ||
wchar_t wDstPath[MAX_PATH]; | ||
|
||
MultiByteToWideChar(CP_UTF8, 0, srcPath, -1, wSrcPath, MAX_PATH); | ||
MultiByteToWideChar(CP_UTF8, 0, dstPath, -1, wDstPath, MAX_PATH); | ||
|
||
// If the source path is relative, prepend the current working directory | ||
if (!do_isabsolute(src)) | ||
{ | ||
// Get the current working directory | ||
wchar_t cwd[MAX_PATH]; | ||
GetCurrentDirectoryW(MAX_PATH, cwd); | ||
|
||
// Convert the source path to a relative path | ||
wchar_t relSrcPath[MAX_PATH]; | ||
swprintf(relSrcPath, MAX_PATH, L"%c:%s", cwd[0], wSrcPath); | ||
|
||
BOOLEAN res = CreateSymbolicLinkW(wDstPath, relSrcPath, SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); | ||
return res != 0; | ||
} | ||
else | ||
{ | ||
BOOLEAN res = CreateSymbolicLinkW(wDstPath, wSrcPath, SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); | ||
return res != 0; | ||
} | ||
#else | ||
int res = symlink(src, dst); | ||
return res == 0; | ||
#endif | ||
} | ||
|
||
int os_linkfile(lua_State* L) | ||
{ | ||
const char* src = luaL_checkstring(L, 1); | ||
const char* dst = luaL_checkstring(L, 2); | ||
|
||
int result = do_linkfile(L, src, dst); | ||
if (!result) | ||
{ | ||
lua_pushnil(L); | ||
lua_pushfstring(L, "Unable to create link from '%s' to '%s'", src, dst); | ||
return 2; | ||
} | ||
|
||
lua_pushboolean(L, 1); | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Creates a new symbolic link to a directory. | ||
|
||
```lua | ||
os.linkdir("src", "dst") | ||
``` | ||
|
||
### Parameters ### | ||
|
||
`src` is the path of the directory to create a symbolic link to. | ||
`dst` is the path to the created symbolic link. | ||
|
||
### Return Value ### | ||
|
||
True if successful, otherwise nil and an error message. | ||
|
||
### Availability ### | ||
|
||
Premake 5.0-beta4 or later. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Creates a new symbolic link to a file. | ||
|
||
```lua | ||
os.linkfile("src", "dst") | ||
``` | ||
|
||
### Parameters ### | ||
|
||
`src` is the path of the file to create a symbolic link to. | ||
`dst` is the path to the created symbolic link. | ||
|
||
### Return Value ### | ||
|
||
True if successful, otherwise nil and an error message. | ||
|
||
### Availability ### | ||
|
||
Premake 5.0-beta4 or later. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters