1
1
/****************************************************************************
2
- * apps/interpreters/python/mount_modules .c
2
+ * apps/interpreters/python/python_wrapper .c
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*
41
41
#include <fcntl.h>
42
42
#include <dirent.h>
43
43
#include <errno.h>
44
+ #include <debug.h>
44
45
45
46
#include <nuttx/drivers/ramdisk.h>
46
47
47
48
#include "romfs_cpython_modules.h"
48
49
50
+ #include "Python.h"
51
+
49
52
/****************************************************************************
50
53
* Pre-processor Definitions
51
54
****************************************************************************/
61
64
#endif
62
65
63
66
#ifndef CONFIG_CPYTHON_ROMFS_MOUNTPOINT
64
- # define CONFIG_CPYTHON_ROMFS_MOUNTPOINT "/usr/local/lib/ "
67
+ # define CONFIG_CPYTHON_ROMFS_MOUNTPOINT "/usr/local/lib"
65
68
#endif
66
69
67
70
#ifdef CONFIG_DISABLE_MOUNTPOINT
85
88
* Private Data
86
89
****************************************************************************/
87
90
88
- /****************************************************************************
89
- * Private Functions
90
- ****************************************************************************/
91
+ /* _PyRuntime occupies around 100kB of RAM in .data region, making it hard to
92
+ * fit the interpreter into the available static RAM. Some devices may have
93
+ * more RAM available on external PSRAM modules that requires to be properly
94
+ * initialized before reading/writing data to it. Thus, _PyRuntime can be
95
+ * moved to bss section and initialized before Python interpreter is started.
96
+ */
97
+
98
+ extern void _PyRuntime_Early_Init (void );
91
99
92
100
/****************************************************************************
93
- * Public Functions
101
+ * Private Functions
94
102
****************************************************************************/
95
103
96
104
/****************************************************************************
97
- * Name: mount_modules
105
+ * Name: check_and_mount_romfs
106
+ *
107
+ * Description:
108
+ * Check if the ROMFS is already mounted, and if not, mount it.
109
+ *
110
+ * Input Parameters:
111
+ * None
112
+ *
113
+ * Returned Value:
114
+ * 0 on success, 1 on failure
115
+ *
98
116
****************************************************************************/
99
117
100
- int main ( int argc , FAR char * argv [] )
118
+ static int check_and_mount_romfs ( void )
101
119
{
102
- int ret ;
120
+ int ret = OK ;
103
121
struct boardioc_romdisk_s desc ;
122
+ FILE * fp ;
123
+ char line [256 ];
124
+ int is_mounted = 0 ;
125
+
126
+ /* Check if the device is already mounted */
104
127
105
- /* Create a RAM disk for the test */
128
+ fp = fopen ("/proc/fs/mount" , "r" );
129
+ if (fp == NULL )
130
+ {
131
+ printf ("ERROR: Failed to open /proc/fs/mount\n" );
132
+ UNUSED (desc );
133
+ return ret = ERROR ;
134
+ }
135
+
136
+ while (fgets (line , sizeof (line ), fp ))
137
+ {
138
+ if (strstr (line , CONFIG_CPYTHON_ROMFS_MOUNTPOINT ) != NULL )
139
+ {
140
+ is_mounted = 1 ;
141
+ break ;
142
+ }
143
+ }
144
+
145
+ fclose (fp );
146
+
147
+ if (is_mounted )
148
+ {
149
+ _info ("Device is already mounted at %s\n" , CONFIG_CPYTHON_ROMFS_MOUNTPOINT );
150
+ UNUSED (desc );
151
+ return ret ;
152
+ }
153
+
154
+ /* Create a RAM disk */
106
155
107
156
desc .minor = CONFIG_CPYTHON_ROMFS_RAMDEVNO ; /* Minor device number of the ROM disk. */
108
157
desc .nsectors = NSECTORS (romfs_cpython_modules_img_len ); /* The number of sectors in the ROM disk */
@@ -119,8 +168,8 @@ int main(int argc, FAR char *argv[])
119
168
120
169
/* Mount the test file system */
121
170
122
- printf ("Mounting ROMFS filesystem at target=%s with source=%s\n" ,
123
- CONFIG_CPYTHON_ROMFS_MOUNTPOINT , MOUNT_DEVNAME );
171
+ _info ("Mounting ROMFS filesystem at target=%s with source=%s\n" ,
172
+ CONFIG_CPYTHON_ROMFS_MOUNTPOINT , MOUNT_DEVNAME );
124
173
125
174
ret = mount (MOUNT_DEVNAME , CONFIG_CPYTHON_ROMFS_MOUNTPOINT , "romfs" ,
126
175
MS_RDONLY , NULL );
@@ -132,3 +181,30 @@ int main(int argc, FAR char *argv[])
132
181
133
182
return 0 ;
134
183
}
184
+
185
+ /****************************************************************************
186
+ * Public Functions
187
+ ****************************************************************************/
188
+
189
+ /****************************************************************************
190
+ * Name: python_wrapper_main
191
+ ****************************************************************************/
192
+
193
+ int main (int argc , FAR char * argv [])
194
+ {
195
+ int ret ;
196
+
197
+ ret = check_and_mount_romfs ();
198
+ if (ret != 0 )
199
+ {
200
+ return ret ;
201
+ }
202
+
203
+ _PyRuntime_Early_Init ();
204
+
205
+ setenv ("PYTHONHOME" , "/usr/local" , 1 );
206
+
207
+ setenv ("PYTHON_BASIC_REPL" , "1" , 1 );
208
+
209
+ return Py_BytesMain (argc , argv );
210
+ }
0 commit comments