sim: switch working directory

If this option is enabled, the working path of nuttx will be modified to the folder where the nuttx file is located.

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
This commit is contained in:
yinshengkai 2023-04-14 14:47:27 +08:00 committed by Xiang Xiao
parent f1b646efc7
commit b705d9b1d5
6 changed files with 102 additions and 0 deletions

View File

@ -159,6 +159,17 @@ config SIM_HOSTFS
---help---
Access host filesystem through HostFS.
config SIM_IMAGEPATH_AS_CWD
bool "Simulator switch working directory"
default n
---help---
If this option is enabled, the working path of nuttx will be modified
to the folder where the nuttx file is located.
It affects the file access of hostfs, which will start looking for
files based on the nuttx image folder.
Otherwise the default $CWD will be used as the starting path for the search.
Absolute paths are never affected.
choice
prompt "Simulated Network Interface"
default SIM_NETDEV

View File

@ -39,6 +39,7 @@ NXSYMBOLS(backtrace)
NXSYMBOLS(bind)
NXSYMBOLS(calloc)
NXSYMBOLS(chmod)
NXSYMBOLS(chdir)
NXSYMBOLS(chown)
NXSYMBOLS(clock_gettime)
NXSYMBOLS(close)
@ -106,6 +107,7 @@ NXSYMBOLS(pthread_sigmask)
NXSYMBOLS(puts)
NXSYMBOLS(read)
NXSYMBOLS(readdir)
NXSYMBOLS(readlink)
NXSYMBOLS(readv)
NXSYMBOLS(realloc)
NXSYMBOLS(recvfrom)

View File

@ -22,10 +22,19 @@
* Included Files
****************************************************************************/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "sim_internal.h"
#ifdef CONFIG_HOST_MACOS
#include <sys/syslimits.h>
#include <mach-o/dyld.h>
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
@ -88,3 +97,38 @@ int host_backtrace(void** array, int size)
return backtrace(array, size);
#endif
}
/****************************************************************************
* Name: host_init_cwd
****************************************************************************/
#ifdef CONFIG_SIM_IMAGEPATH_AS_CWD
void host_init_cwd(void)
{
char *name;
char path[PATH_MAX];
int len = PATH_MAX;
/* Get the absolute path of the executable file */
# ifdef CONFIG_HOST_LINUX
len = readlink("/proc/self/exe", path, len);
if (len < 0)
{
perror("readlink failed");
return;
}
# else
if (_NSGetExecutablePath(path, &len) < 0)
{
perror("_NSGetExecutablePath failed");
return;
}
# endif
path[len] = '\0';
name = strrchr(path, '/');
*++name = '\0';
chdir(path);
}
#endif

View File

@ -254,6 +254,10 @@ static int sim_loop_task(int argc, char **argv)
void up_initialize(void)
{
#ifdef CONFIG_SIM_IMAGEPATH_AS_CWD
host_init_cwd();
#endif
#ifdef CONFIG_PM
/* Initialize the power management subsystem. This MCU-specific function
* must be called *very* early in the initialization sequence *before* any

View File

@ -164,6 +164,10 @@ void *sim_doirq(int irq, void *regs);
void host_abort(int status);
int host_backtrace(void** array, int size);
#ifdef CONFIG_SIM_IMAGEPATH_AS_CWD
void host_init_cwd(void);
#endif
/* sim_hostmemory.c *********************************************************/
void *host_allocheap(size_t sz);

View File

@ -22,7 +22,15 @@
* Included Files
****************************************************************************/
#include <direct.h>
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <windows.h>
#include <libloaderapi.h>
#include "sim_internal.h"
/****************************************************************************
* Public Functions
@ -47,3 +55,32 @@ int host_backtrace(void** array, int size)
{
return CaptureStackBackTrace(0, size, array, NULL);
}
/****************************************************************************
* Name: host_init_cwd
****************************************************************************/
#ifdef CONFIG_SIM_IMAGEPATH_AS_CWD
void host_init_cwd(void)
{
char *name;
char path[MAX_PATH];
/* Get the absolute path of the executable file */
if (GetModuleFileNameA(GetModuleHandleA(NULL), path, MAX_PATH) == 0)
{
perror("GetModuleFileNameA failed");
return;
}
name = strrchr(path, '/');
if (name == NULL)
{
name = strrchr(path, '\\');
}
*++name = '\0';
_chdir(path);
}
#endif