Ponup / php-sdl

SDL2 bindings for the PHP language
https://pecl.php.net/package/sdl
MIT License
106 stars 20 forks source link

How to build on Windows 8/8.1/10/11 #52

Open zorobabel opened 2 years ago

zorobabel commented 2 years ago

Hi!

I tried to compile the project to generate the DLL without luck, just got very very strange errors.

Please, can you share the right instructions?

BTW, compiling on Ubuntu is a breeze :D

zorobabel commented 2 years ago

Hello!

Well, I made a last try with success, following the official guide "Build your own PHP on Windows", so it's recommended to read it first to get an overview of the build process.

1. Check the operating system

I use is Windows 8.1, so this guide will work on 8.0, 10 or 11.

2. Install the IDE

Visual Studio 2019 is required. The VS website will make you download the installer for Visual Studio Installer. Install and start it to really install the IDE, which will be in Available tab (the Community version is ok).

3. Install the compiler

After previous step, inside Visual Studio Installer, in Installed tab, click on Modify button of Visual Studio Community 2019 element. Then, in Workloads tab, check Desktop development with C++ element, the right panel Installation details will be updated and uncheck all elements except MSVC v142 Build Tools - VS 2019 C++ for x64/x86 (more recent). In Individual components tab, write c++ in search box and check C++ core features, C++ 2019 Redistributable update, MSVC v142 Build Tools - VS 2019 C++ for x64/x86 (more recent), MSVC v142 - VS 2019 C++ Build Tools for x64/x86 (v14.20). The right panel will look like this:

Installation details

Visual Studio code editor

Desktop development with C++ ​ Included ​ ✓ Visual C++ core desktop features ​ Optional ​ ☑ MSVC v142 Build Tools - VS 2019 C++ for x64/x86 (more recent)Individual components ​ ☑ MSVC v142 - VS 2019 C++ Build Tools for x64/x86 (v14.20)

Click on Modify button located at the bottom to proceed.

4. Set up the PHP SDK

Download it from this link.

Extract it and rename the folder to php-sdk (I placed the folder at D:\php-sdk).

5. Set up the build directory

Open a terminal inside the folder (I recommend ConEmu).

Invoke the starter script:

phpsdk-vs16-x64.bat

Create the directory structure:

phpsdk_buildtree phpdev

Now you will be located at D:\php-sdk\phpdev\vs16\x64.

6. Set up PHP

This guide is for PHP 8.0.11 x64 Non Thread Safe.

Download the source code from this link.

Extract it D:\php-sdk\phpdev\vs16\x64 (at same level as deps folder).

Change the current directory:

cd php-8.0.11-src

Now you will be located at D:\php-sdk\phpdev\vs16\x64\php-8.0.11-src.

Download dependencies:

phpsdk_deps -u -b 8.0

Dependencies are located on a previous level, in deps folder (which size is aprox. 301+ MB).

7. Set up SDL

Download the development version from this link.

Extract it and move the content of the folders as follows (I placed the folder at D:\SDL):

D:\SDL\includeD:\php-sdk\phpdev\vs16\x64\deps\include D:\SDL\lib\x64\*.libD:\php-sdk\phpdev\vs16\x64\deps\lib D:\SDL\lib\x64\SDL2.dllD:\php-sdk\phpdev\vs16\x64\deps\bin

8. Set up PHP-SDL

Download it from this link.

Move it to D:\php-sdk\phpdev\vs16\x64\php-8.0.11-src\ext.

Extract it and rename the folder to sdl.

Move the content of D:\php-sdk\phpdev\vs16\x64\php-8.0.11-src\ext\sdl\src one level up to D:\php-sdk\phpdev\vs16\x64\php-8.0.11-src\ext\sdl. After this, delete the src folder.

Inside D:\php-sdk\phpdev\vs16\x64\php-8.0.11-src\ext\sdl create a file named config.w32 with the following content:

// vim:ft=javascript

ARG_ENABLE('sdl', 'Whether to enable SDL2 support', 'no');

if(PHP_SDL != 'no') {
    CHECK_LIB("SDL2.lib", "sdl", PHP_SDL);
    CHECK_LIB("SDL2main.lib", "sdl", PHP_SDL);
    CHECK_LIB("SDL2test.lib", "sdl", PHP_SDL);

    var sdl_sources = 'php_sdl.c blendmode.c cpuinfo.c error.c event.c filesystem.c glcontext.c joystick.c keyboard.c messagebox.c mouse.c mutex.c pixels.c platform.c power.c rect.c render.c rwops.c sdl.c shape.c surface.c timer.c version.c video.c window.c';

    EXTENSION('sdl', sdl_sources, PHP_SDL_SHARED);

    var sdl_headers = 'php_sdl.h blendmode.h cpuinfo.h error.h event.h filesystem.h glcontext.h joystick.h keyboard.h messagebox.h mouse.h mutex.h pixels.h platform.h power.h rect.h render.h rwops.h sdl.h shape.h surface.h timer.h version.h video.h window.h';

    PHP_INSTALL_HEADERS('ext/sdl', sdl_headers);
    AC_DEFINE('HAVE_SDL', 1);
}

Edit the D:\php-sdk\phpdev\vs16\x64\php-8.0.11-src\ext\sdl\surface.c file and change the lines from:

result = SDLCALL SDL_SaveBMP_RW(surface, rwops, 0);

To:

result = SDL_SaveBMP_RW(surface, rwops, 0);

9. Compile

In your terminal, where you stayed, i.e., D:\php-sdk\phpdev\vs16\x64\php-8.0.11-src folder, execute the following commands:

buildconf
configure --disable-all --disable-zts --enable-cli --enable-sdl=shared
nmake
nmake snap

You will find the long wished DLL at following locations (are the same):

D:\php-sdk\phpdev\vs16\x64\php-8.0.11-src\x64\Release\php_sdl.dll D:\php-sdk\phpdev\vs16\x64\php-8.0.11-src\x64\Release\php-8.0.11\ext\php_sdl.dll

10. Test

Download a fresh copy of PHP 8.0.11 x64 Non Thread Safe from this link.

Extract it and rename the folder to PHP (I placed the folder at D:\PHP).

Copy the DLLs:

D:\php-sdk\phpdev\vs16\x64\deps\bin\SDL2.dllD:\PHP D:\php-sdk\phpdev\vs16\x64\php-8.0.11-src\x64\Release\php-8.0.11\ext\php_sdl.dllD:\PHP\ext

Copy and rename the required INI file:

D:\PHP\php.ini-developmentD:\PHP\php.ini

Edit D:\PHP\php.ini uncommenting the line:

extension_dir = "ext"

Edit D:\PHP\php.ini adding the line:

...
;extension=pgsql
extension=sdl
;extension=shmop
...

Open a new terminal and execute:

D:\PHP\php.exe D:\php-sdk\phpdev\vs16\x64\php-8.0.11-src\ext\sdl\examples\005-draw-rectangle.php

You have to view the following window:

005-draw-rectangle

That's all.


Note: You can download the DLLs from this link.

zorobabel commented 2 years ago

@santiagolizardo I'm intrigued about removing SDLCALL calls in surface.c because I'm not a C/C++ expert, so is there any way to change those lines to make the extension able to compile on Linux/macOS/Windows? Thanks.

heckermanmajo commented 1 year ago

@zorobabel Maybe with macros like #ifdef MINGW32 https://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros