Skip to content

Commit

Permalink
Better find json logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandro Kalatozishvili committed Aug 16, 2023
1 parent 570df5f commit 85c1bdf
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 68 deletions.
43 changes: 32 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,46 @@ Example:

"find": {
"libssl.so:libcrypto.so": {
"path": "/usr/local/ssl/lib:/usr/local/ssl/lib64",
"flags": "-D_PROJ_USE_SSL",
"libs": "-lssl -lcrypto"
"found": {
"append" : {
"path": "/usr/local/ssl/lib:/usr/local/ssl/lib64",
"flags": "-D_PROJ_USE_SSL",
"libs": "-lssl -lcrypto"
}
}
},

"libz.so": {
"flags": "-D_PROJ_USE_LIBZ",
"libs": "-lz"
"found": {
"append" : {
"flags": "-D_PROJ_USE_LIBZ",
"libs": "-lz"
}
}
},

"any_file.txt": {
"path": "/opt/examples/smake",
"flags": "-D_OPTIONAL_FLAGS",
"libs": "-loptional -lexample",
"thisPathOnly": true,
"insensitive": false,
"recursive": false
"recursive": false,

"found": {
"append" : {
"flags": "-D_OPTIONAL_FLAGS",
"libs": "-loptional -lexample"
}
},

"notFound": {
"append": {
"flags": "-D_NO_OPTIONAL_FLAGS",
},

"set": {
"libs": "-lonlythis"
}
}
}
}
},
Expand All @@ -150,13 +173,11 @@ As you can see in the example above, `find` JSON object can be used to find file
The keys in the nested objects describe how `smake` should handle each dependency:

- `path` (optional): A colon-separated list of directories where `smake` should look for the files. If a file is found in these directories, the corresponding flags and libs will be applied.
- `flags` (optional): Flags that should be added to the compiler command line if the file is found.
- `libs` (optional): Libraries that should be linked to the executable if the file is found.
- `thisPathOnly` (optional): If set to true, smake will only look for the file in the specified path and not in the default locations.
- `insensitive` (optional): If set to true, the file search will be case-insensitive.
- `recursive` (optional): If set to true, smake will search recursively in the directories specified by path.

In the example above, `smake` will try to find `libssl.so` and `libcrypto.so` in either `/usr/local/ssl/lib` or `/usr/local/ssl/lib64`, if both of them are found, it will add `-D_PROJ_USE_SSL` to the compiler flags and `-lssl -lcrypto` to the linked libraries. The options for `libz.so` and `any_file.txt` are handled in a similar manner, with the additional `thisPathOnly`, `insensitive`, and `recursive` options.
In the example above, `smake` will try to find `libssl.so` and `libcrypto.so` in either `/usr/local/ssl/lib` or `/usr/local/ssl/lib64`, if both of them are found, it will append `-D_PROJ_USE_SSL` to the compiler flags and `-lssl -lcrypto` to the linked libraries. The options for `libz.so` and `any_file.txt` are handled in a similar manner, with the additional `thisPathOnly`, `insensitive`, and `recursive` options.

Without `thisPathOnly` option, `smake` will first look for files in the provided `path`. If not found there, it will search in the following default locations:

Expand Down
8 changes: 6 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ for arg in "$@"; do
if [[ $arg == "--install" ]]; then
sudo make install
fi
done

if [[ $arg == "--cleanup" ]]; then
./clean.sh
for arg in "$@"; do
if [[ $arg == "--cleanup" ||
$arg == "--clean" ]]; then
$PROJ_PATH/clean.sh
fi
done

119 changes: 82 additions & 37 deletions src/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,37 @@ xbool_t SMake_AddTokens(xarray_t *pArr, const char *pDlmt, const char *pInput)
return XSTDOK;
}

static xbool_t SMake_AddFindObject(smake_ctx_t *pCtx, xjson_obj_t *pFindObj, xbool_t bAppend)
{
xjson_obj_t *pFlagsObj = XJSON_GetObject(pFindObj, "flags");
xjson_obj_t *pLibsObj = XJSON_GetObject(pFindObj, "libs");
xjson_obj_t *pLdObj = XJSON_GetObject(pFindObj, "ldLibs");

const char *pFlags = pFlagsObj != NULL ? XJSON_GetString(pFlagsObj) : NULL;
const char *pLibs = pLibsObj != NULL ? XJSON_GetString(pLibsObj) : NULL;
const char *pLd = pLdObj != NULL ? XJSON_GetString(pLdObj) : NULL;

if (xstrused(pFlags))
{
if (!bAppend) XArray_Clear(&pCtx->flagArr);
SMake_AddTokens(&pCtx->flagArr, XSTR_SPACE, pFlags);
}

if (xstrused(pLibs))
{
if (!bAppend) XArray_Clear(&pCtx->libArr);
SMake_AddTokens(&pCtx->libArr, XSTR_SPACE, pLibs);
}

if (xstrused(pLd))
{
if (!bAppend) XArray_Clear(&pCtx->ldArr);
SMake_AddTokens(&pCtx->ldArr, XSTR_SPACE, pLd);
}

return XTRUE;
}

int SMake_ParseArgs(smake_ctx_t *pCtx, int argc, char *argv[])
{
int nChar = 0;
Expand Down Expand Up @@ -330,6 +361,27 @@ int SMake_ParseConfig(smake_ctx_t *pCtx)
}
}

pValueObj = XJSON_GetObject(pBuildObj, "flags");
if (pValueObj != NULL)
{
const char *pFlags = XJSON_GetString(pValueObj);
SMake_AddTokens(&pCtx->flagArr, XSTR_SPACE, pFlags);
}

pValueObj = XJSON_GetObject(pBuildObj, "libs");
if (pValueObj != NULL)
{
const char *pLibs = XJSON_GetString(pValueObj);
SMake_AddTokens(&pCtx->libArr, XSTR_SPACE, pLibs);
}

pValueObj = XJSON_GetObject(pBuildObj, "ldLibs");
if (pValueObj != NULL)
{
const char *pLd = XJSON_GetString(pValueObj);
SMake_AddTokens(&pCtx->ldArr, XSTR_SPACE, pLd);
}

xjson_obj_t *pFindObj = XJSON_GetObject(pBuildObj, "find");
if (pFindObj != NULL)
{
Expand All @@ -340,57 +392,50 @@ int SMake_ParseConfig(smake_ctx_t *pCtx)
for (i = 0; i < nUsed; i++)
{
xmap_pair_t *pPair = (xmap_pair_t*)XArray_GetData(pObjects, i);
if (pPair == NULL || !xstrused(pPair->pKey)) continue;
if (pPair == NULL || pPair->pData == NULL|| !xstrused(pPair->pKey)) continue;

xjson_obj_t *pFoundObj = XJSON_GetObject((xjson_obj_t*)pPair->pData, "found");
xjson_obj_t *pNotFoundObj = XJSON_GetObject((xjson_obj_t*)pPair->pData, "notFound");
if (pFoundObj == NULL && pNotFoundObj == NULL) continue;

smake_find_t finder;
finder.pFindStr = pPair->pKey;

xjson_obj_t *pBoolObj = XJSON_GetObject((xjson_obj_t*)pPair->pData, "recursive");
finder.bRecursive = pBoolObj != NULL ? XJSON_GetBool(pBoolObj) : XTRUE;
xjson_obj_t *pFindOptObj = XJSON_GetObject((xjson_obj_t*)pPair->pData, "path");
finder.pPath = pFindOptObj != NULL ? XJSON_GetString(pFindOptObj) : NULL;

pFindOptObj = XJSON_GetObject((xjson_obj_t*)pPair->pData, "thisPathOnly");
finder.bThisPathOnly = pFindOptObj != NULL ? XJSON_GetBool(pFindOptObj) : XFALSE;

pBoolObj = XJSON_GetObject((xjson_obj_t*)pPair->pData, "insensitive");
finder.bInsensitive = pBoolObj != NULL ? XJSON_GetBool(pBoolObj) : XTRUE;
pFindOptObj = XJSON_GetObject((xjson_obj_t*)pPair->pData, "insensitive");
finder.bInsensitive = pFindOptObj != NULL ? XJSON_GetBool(pFindOptObj) : XTRUE;

pBoolObj = XJSON_GetObject((xjson_obj_t*)pPair->pData, "thisPathOnly");
finder.bThisPathOnly = pBoolObj != NULL ? XJSON_GetBool(pBoolObj) : XFALSE;
pFindOptObj = XJSON_GetObject((xjson_obj_t*)pPair->pData, "recursive");
finder.bRecursive = pFindOptObj != NULL ? XJSON_GetBool(pFindOptObj) : XTRUE;

XSTATUS nStatus = SMake_FindLibs(pCtx, &finder);
if (nStatus == XSTDOK)
{
xjson_obj_t *pAppendObj = XJSON_GetObject(pFoundObj, "append");
if (pAppendObj != NULL) SMake_AddFindObject(pCtx, pAppendObj, XTRUE);

xjson_obj_t *pFlagsObj = XJSON_GetObject((xjson_obj_t*)pPair->pData, "flags");
xjson_obj_t *pLibsObj = XJSON_GetObject((xjson_obj_t*)pPair->pData, "libs");
xjson_obj_t *pPathObj = XJSON_GetObject((xjson_obj_t*)pPair->pData, "path");
xjson_obj_t *pLdObj = XJSON_GetObject((xjson_obj_t*)pPair->pData, "ldLibs");
xjson_obj_t *pSetObj = XJSON_GetObject(pFoundObj, "set");
if (pSetObj != NULL) SMake_AddFindObject(pCtx, pSetObj, XFALSE);
}
else
{
xjson_obj_t *pAppendObj = XJSON_GetObject(pNotFoundObj, "append");
if (pAppendObj != NULL) SMake_AddFindObject(pCtx, pAppendObj, XTRUE);

finder.pFlags = pFlagsObj != NULL ? XJSON_GetString(pFlagsObj) : NULL;
finder.pLibs = pLibsObj != NULL ? XJSON_GetString(pLibsObj) : NULL;
finder.pPath = pPathObj != NULL ? XJSON_GetString(pPathObj) : NULL;
finder.pLd = pLdObj != NULL ? XJSON_GetString(pLdObj) : NULL;
SMake_FindLibs(pCtx, &finder);
xjson_obj_t *pSetObj = XJSON_GetObject(pNotFoundObj, "set");
if (pSetObj != NULL) SMake_AddFindObject(pCtx, pSetObj, XFALSE);
}
}

XArray_Destroy(pObjects);
}
}

pValueObj = XJSON_GetObject(pBuildObj, "flags");
if (pValueObj != NULL)
{
const char *pFlags = XJSON_GetString(pValueObj);
SMake_AddTokens(&pCtx->flagArr, XSTR_SPACE, pFlags);
}

pValueObj = XJSON_GetObject(pBuildObj, "libs");
if (pValueObj != NULL)
{
const char *pLibs = XJSON_GetString(pValueObj);
SMake_AddTokens(&pCtx->libArr, XSTR_SPACE, pLibs);
}

pValueObj = XJSON_GetObject(pBuildObj, "ldLibs");
if (pValueObj != NULL)
{
const char *pLd = XJSON_GetString(pValueObj);
SMake_AddTokens(&pCtx->ldArr, XSTR_SPACE, pLd);
}

pValueObj = XJSON_GetObject(pBuildObj, "name");
if (pValueObj != NULL) xstrncpy(pCtx->sName, sizeof(pCtx->sName), XJSON_GetString(pValueObj));

Expand Down
14 changes: 0 additions & 14 deletions src/find.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ static XSTATUS SMake_FindLib(const smake_find_t *pFind, const char *pLib)
XSTATUS SMake_FindLibs(smake_ctx_t *pCtx, const smake_find_t *pFind)
{
XASSERT_RET((pFind != NULL && xstrused(pFind->pFindStr)), XSTDINV);
XASSERT_RET((xstrused(pFind->pFlags) || xstrused(pFind->pLibs)), XSTDNON);

xarray_t *pLibs = xstrsplit(pFind->pFindStr, ":");
XASSERT(pLibs, xthrow("Failed to split input: %s", pFind->pFindStr));

Expand All @@ -97,18 +95,6 @@ XSTATUS SMake_FindLibs(smake_ctx_t *pCtx, const smake_find_t *pFind)
if (nStatus != XSTDOK) break;
}

if (nStatus == XSTDOK)
{
if (xstrused(pFind->pFlags))
SMake_AddTokens(&pCtx->flagArr, XSTR_SPACE, pFind->pFlags);

if (xstrused(pFind->pLibs))
SMake_AddTokens(&pCtx->libArr, XSTR_SPACE, pFind->pLibs);

if (xstrused(pFind->pLd))
SMake_AddTokens(&pCtx->ldArr, XSTR_SPACE, pFind->pLd);
}

XArray_Destroy(pLibs);
return nStatus;
}
3 changes: 0 additions & 3 deletions src/find.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@

typedef struct SMakeFind {
const char *pFindStr;
const char *pFlags;
const char *pLibs;
const char *pPath;
const char *pLd;
xbool_t bThisPathOnly;
xbool_t bInsensitive;
xbool_t bRecursive;
Expand Down
2 changes: 1 addition & 1 deletion src/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#define SMAKE_VERSION_MAX 1
#define SMAKE_VERSION_MIN 1
#define SMAKE_BUILD_NUMBER 16
#define SMAKE_BUILD_NUMBER 17

#ifndef _SMAKE_VERSION_H_
#define _SMAKE_VERSION_H_
Expand Down

0 comments on commit 85c1bdf

Please sign in to comment.