Skip to content

Commit 577ec60

Browse files
committed
feat: Migrate from GCC to LLVM/Clang as the primary compiler
- Replaced GCC 15.2.0 with LLVM/Clang 21.1.8 for the vitasdk project. - Updated cross-compilation tools to use LLVM binaries instead of GCC. - Commented out GCC-specific dependencies and build steps, retaining them for reference. - Integrated LLVM/Clang build process, including runtime libraries (compiler-rt, libc++, libc++abi). - Updated CMake configuration to accommodate LLVM build requirements. - Added wrapper scripts for clang to handle Vita-specific libraries. - Created a detailed migration document outlining changes, current status, and known issues. - Adjusted patches for newlib and pthread-embedded to ensure compatibility with LLVM.
1 parent f5ecc60 commit 577ec60

7 files changed

Lines changed: 589 additions & 171 deletions

CMakeLists.txt

Lines changed: 303 additions & 170 deletions
Large diffs are not rendered by default.

LLVM_MIGRATION.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Migración de GCC a LLVM/Clang
2+
3+
Esta rama (`llvm`) contiene una migración experimental del vitasdk de GCC a LLVM/Clang como compilador principal.
4+
5+
## Cambios principales
6+
7+
### 1. Reemplazo del compilador
8+
- **Antes**: GCC 15.2.0
9+
- **Ahora**: LLVM/Clang 19.1.7
10+
11+
### 2. Herramientas de compilación cruzada
12+
Las siguientes variables han sido actualizadas para usar LLVM:
13+
14+
- `CC_FOR_TARGET`: `clang` (antes `arm-vita-eabi-gcc`)
15+
- `CXX_FOR_TARGET`: `clang++` (antes `arm-vita-eabi-g++`)
16+
- `AR_FOR_TARGET`: `llvm-ar` (antes `arm-vita-eabi-ar`)
17+
- `RANLIB_FOR_TARGET`: `llvm-ranlib` (antes `arm-vita-eabi-ranlib`)
18+
- `OBJCOPY_FOR_TARGET`: `llvm-objcopy` (antes `arm-vita-eabi-objcopy`)
19+
- `OBJDUMP_FOR_TARGET`: `llvm-objdump` (antes `arm-vita-eabi-objdump`)
20+
- `NM_FOR_TARGET`: `llvm-nm` (antes `arm-vita-eabi-nm`)
21+
- `STRIP_FOR_TARGET`: `llvm-strip` (antes `arm-vita-eabi-strip`)
22+
- `READELF_FOR_TARGET`: `llvm-readelf` (antes `arm-vita-eabi-readelf`)
23+
24+
### 3. Binutils
25+
Se mantiene binutils para las siguientes herramientas:
26+
- `ld` (linker) - Se usa el linker de binutils por compatibilidad
27+
- `as` (assembler) - Se usa el ensamblador de binutils por compatibilidad
28+
29+
Nota: LLD (el linker de LLVM) está disponible pero no se usa por defecto en esta primera versión.
30+
31+
### 4. Componentes LLVM incluidos
32+
- **clang**: Compilador de C/C++
33+
- **lld**: Linker (disponible pero no usado por defecto)
34+
- **compiler-rt**: Runtime del compilador para builtins ARM
35+
36+
### 5. Configuración de LLVM
37+
```cmake
38+
-DLLVM_TARGETS_TO_BUILD=ARM
39+
-DLLVM_DEFAULT_TARGET_TRIPLE=arm-vita-eabi
40+
-DLLVM_ENABLE_PROJECTS=clang;lld;compiler-rt
41+
-DCOMPILER_RT_BUILD_BUILTINS=ON
42+
-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
43+
```
44+
45+
### 6. Dependencias
46+
LLVM requiere menos dependencias de build que GCC:
47+
- ✅ Mantiene: zlib
48+
- ❌ No requiere: GMP, MPFR, MPC, ISL (solo para GCC)
49+
50+
## Estado actual
51+
52+
### ✅ Completado
53+
- [x] Configuración básica de LLVM
54+
- [x] Integración de clang como compilador principal
55+
- [x] Actualización de variables de herramientas de compilación cruzada
56+
- [x] Actualización de dependencias (newlib, pthread-embedded, vita-headers)
57+
- [x] Comentar código de GCC (preservado para referencia)
58+
59+
### 🚧 En pruebas
60+
- [ ] Compilación completa del vitasdk en macOS
61+
- [ ] Verificación de compatibilidad con vita-headers
62+
- [ ] Compilación de samples
63+
- [ ] Pruebas en otras plataformas (Linux, Windows)
64+
65+
### 📋 Por hacer
66+
- [ ] Optimizar flags de compilación para ARM Cortex-A9
67+
- [ ] Evaluar el uso de LLD en lugar de ld de binutils
68+
- [ ] Documentar diferencias de comportamiento con GCC
69+
- [ ] Benchmarks de rendimiento GCC vs LLVM
70+
71+
## Compilación
72+
73+
### macOS (nativo)
74+
```bash
75+
mkdir build
76+
cd build
77+
cmake ..
78+
make -j$(sysctl -n hw.ncpu)
79+
```
80+
81+
### Linux (nativo)
82+
```bash
83+
mkdir build
84+
cd build
85+
cmake ..
86+
make -j$(nproc)
87+
```
88+
89+
## Notas técnicas
90+
91+
### Diferencias de flags con GCC
92+
LLVM/Clang puede requerir flags diferentes o tener comportamientos distintos:
93+
- Los warnings pueden ser diferentes
94+
- La generación de código puede optimizarse de forma distinta
95+
- Algunas extensiones de GCC pueden no estar disponibles
96+
97+
### Compatibilidad con código existente
98+
- El código que usa extensiones específicas de GCC puede necesitar ajustes
99+
- Las pragmas y attributes pueden comportarse de forma diferente
100+
- Se recomienda probar thoroughly antes de usar en producción
101+
102+
## Problemas conocidos
103+
104+
1. **Primera compilación**: LLVM tarda más en compilar que GCC (especialmente en macOS)
105+
2. **Tamaño del build**: LLVM requiere más espacio de disco durante la compilación
106+
3. **Compatibilidad**: Algunos proyectos pueden requerir ajustes para compilar con Clang
107+
108+
## Contribuir
109+
110+
Si encuentras problemas o mejoras, por favor:
111+
1. Documenta el problema/mejora
112+
2. Crea un issue en el repositorio
113+
3. Propón una solución si es posible
114+
115+
## Referencias
116+
117+
- [LLVM Project](https://llvm.org/)
118+
- [Clang Documentation](https://clang.llvm.org/docs/)
119+
- [VitaSDK Documentation](https://vitasdk.org/)

arm-vita-eabi-gcc-wrapper.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/sh
2+
# Wrapper for arm-vita-eabi-gcc (clang) to add Vita-specific libraries
3+
# This replicates the LIB_SPEC behavior from GCC patch
4+
5+
# Get the directory where this script is located
6+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
7+
8+
# Determine if we are running as C++ compiler
9+
NAME=$(basename "$0")
10+
case "$NAME" in
11+
*g++*|*c++*)
12+
CLANG="${SCRIPT_DIR}/../arm-vita-eabi/bin/clang++"
13+
;;
14+
*)
15+
CLANG="${SCRIPT_DIR}/../arm-vita-eabi/bin/clang"
16+
;;
17+
esac
18+
19+
# Default Vita libraries (from LIB_SPEC in gcc.cc patch)
20+
VITA_LIBS="-lSceRtc_stub -lSceSysmem_stub -lSceKernelThreadMgr_stub -lSceKernelModulemgr_stub -lSceIofilemgr_stub -lSceProcessmgr_stub -lSceLibKernel_stub -lSceNet_stub -lSceNetCtl_stub -lSceSysmodule_stub"
21+
22+
# Check if we're linking (not just compiling)
23+
LINKING=1
24+
for arg in "$@"; do
25+
case "$arg" in
26+
-c|-E|-S)
27+
LINKING=0
28+
break
29+
;;
30+
esac
31+
done
32+
33+
# Execute clang with config file
34+
if [ $LINKING -eq 1 ]; then
35+
# Add Vita libraries when linking
36+
# Check if -pthread is in arguments
37+
PTHREAD_LIBS=""
38+
for arg in "$@"; do
39+
case "$arg" in
40+
-pthread|--pthread)
41+
PTHREAD_LIBS="--whole-archive -lpthread --no-whole-archive"
42+
break
43+
;;
44+
esac
45+
done
46+
47+
exec "${CLANG}" --config "${SCRIPT_DIR}/arm-vita-eabi.cfg" "$@" ${PTHREAD_LIBS} ${VITA_LIBS}
48+
else
49+
# Just compile, no libraries needed
50+
exec "${CLANG}" --config "${SCRIPT_DIR}/arm-vita-eabi.cfg" "$@"
51+
fi

arm-vita-eabi.cfg

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Clang configuration file for PS Vita (arm-vita-eabi)
2+
# This file sets the default compilation flags for the vita target
3+
# Equivalent to GCC patch: patches/gcc/0001-gcc-15.patch
4+
5+
# Target triple
6+
--target=arm-vita-eabi
7+
8+
# Architecture settings (matching GCC configuration and CMakeLists.txt)
9+
-march=armv7-a
10+
-mcpu=cortex-a9
11+
-mfpu=neon
12+
-mfloat-abi=hard
13+
-mthumb
14+
15+
# System defines (equivalent to builtin_define in arm-c.cc)
16+
-D__vita__
17+
18+
# Signed char by default (equivalent to DEFAULT_SIGNED_CHAR=1 in arm.h)
19+
-fsigned-char
20+
21+
# STDINT_LONG32=0: Use int/unsigned int for int32_t/uint32_t (not long)
22+
# This is important for ABI compatibility
23+
-fno-short-wchar
24+
25+
# Newlib specific defines
26+
-D_POSIX_THREADS
27+
-D_UNIX98_THREAD_MUTEX_ATTRIBUTES
28+
29+
# Optimization and code generation
30+
-ffunction-sections
31+
-fdata-sections
32+
-fno-optimize-sibling-calls
33+
34+
# Default libraries (equivalent to LIB_SPEC in gcc.cc)
35+
# These will be linked automatically when using arm-vita-eabi-gcc
36+
# Note: -l flags in .cfg don't work the same way, handled by linker wrapper instead
37+
38+
# Clang runtime library paths
39+
-rtlib=compiler-rt
40+
-L<CFGDIR>/../arm-vita-eabi/lib
41+
42+
# Thread model
43+
-pthread

cmake/strip_host_binaries.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ if(NOT WIN32)
99
execute_process(COMMAND find "${BINDIR}" -maxdepth 1 -perm ${find_perms} -and ! -type d
1010
OUTPUT_VARIABLE binaries
1111
OUTPUT_STRIP_TRAILING_WHITESPACE)
12-
string(REGEX REPLACE "\n" ";" binaries ${binaries})
12+
if(binaries)
13+
string(REGEX REPLACE "\n" ";" binaries ${binaries})
14+
endif()
1315
else()
1416
file(GLOB_RECURSE binaries "${BINDIR}/*exe")
1517
endif()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/newlib/libc/include/stdatomic.h b/newlib/libc/include/stdatomic.h
2+
index 1234567..abcdefg 100644
3+
--- a/newlib/libc/include/stdatomic.h
4+
+++ b/newlib/libc/include/stdatomic.h
5+
@@ -30,6 +30,7 @@
6+
#define _STDATOMIC_H_
7+
8+
#include <sys/cdefs.h>
9+
+#include <stdint.h>
10+
#include <sys/_types.h>
11+
12+
#if __has_extension(c_atomic) || __has_extension(cxx_atomic)

patches/pthread-embedded.patch

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
diff --git a/platform/vita/vita_osal.c b/platform/vita/vita_osal.c
2+
index 23b8bed..99f8137 100644
3+
--- a/platform/vita/vita_osal.c
4+
+++ b/platform/vita/vita_osal.c
5+
@@ -28,6 +28,7 @@
6+
7+
8+
#include <stdio.h>
9+
+#include <stdint.h>
10+
#include <stdlib.h>
11+
#include <string.h>
12+
#include <stdatomic.h>
13+
@@ -530,7 +531,7 @@ pte_osResult pte_osSemaphoreCancellablePend(pte_osSemaphoreHandle semHandle, uns
14+
15+
int pte_osAtomicExchange(int *ptarg, int val)
16+
{
17+
- return atomic_exchange(ptarg, val);
18+
+ return __atomic_exchange_n(ptarg, val, __ATOMIC_SEQ_CST);
19+
}
20+
21+
int pte_osAtomicCompareExchange(int *pdest, int exchange, int comp)
22+
@@ -544,7 +545,7 @@ int pte_osAtomicCompareExchange(int *pdest, int exchange, int comp)
23+
24+
int pte_osAtomicExchangeAdd(int volatile* pAddend, int value)
25+
{
26+
- return atomic_fetch_add(pAddend, value);
27+
+ return __atomic_fetch_add(pAddend, value, __ATOMIC_SEQ_CST);
28+
}
29+
30+
int pte_osAtomicDecrement(int *pdest)
31+
diff --git a/sys/_pthreadtypes.h b/sys/_pthreadtypes.h
32+
index 35aac0e..87b34a4 100644
33+
--- a/sys/_pthreadtypes.h
34+
+++ b/sys/_pthreadtypes.h
35+
@@ -84,7 +84,11 @@ enum
36+
37+
typedef struct pthread_mutex_t_ * pthread_mutex_t;
38+
typedef struct pthread_mutexattr_t_ * pthread_mutexattr_t;
39+
+#ifdef __cplusplus
40+
+#define _PTHREAD_MUTEX_INITIALIZER nullptr
41+
+#else
42+
#define _PTHREAD_MUTEX_INITIALIZER ((pthread_mutex_t) -1)
43+
+#endif
44+
#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER ((pthread_mutex_t) -2)
45+
#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER ((pthread_mutex_t) -3)
46+
47+
@@ -98,7 +102,11 @@ typedef struct pthread_mutexattr_t_ * pthread_mutexattr_t;
48+
49+
typedef struct pthread_cond_t_ * pthread_cond_t;
50+
typedef struct pthread_condattr_t_ * pthread_condattr_t;
51+
+#ifdef __cplusplus
52+
+#define _PTHREAD_COND_INITIALIZER nullptr
53+
+#else
54+
#define _PTHREAD_COND_INITIALIZER ((pthread_cond_t) -1)
55+
+#endif
56+
57+
58+
/* Keys */

0 commit comments

Comments
 (0)