Skip to content

Commit

Permalink
implement extension
Browse files Browse the repository at this point in the history
  • Loading branch information
zeriyoshi committed Aug 2, 2024
1 parent 202fc1c commit ade1dc2
Show file tree
Hide file tree
Showing 20 changed files with 558 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "pskel",
"name": "extension_example",
"customizations": {
"vscode": {
"extensions": [
Expand Down
100 changes: 100 additions & 0 deletions .github/renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:recommended"
],
"timezone": "Asia/Tokyo",
"git-submodules": {
"enabled": true,
"versioning": "git",
"fileMatch": [
"(^|/)\\.gitmodules$"
]
},
"packageRules": [
{
"matchManagers": [
"git-submodules"
],
"enabled": false
},
{
"matchDepNames": [
"jedisct1/libsodium"
],
"matchManagers": [
"regex"
],
"versioning": "regex:^(?<major>\\d+)\\.(?<minor>\\d+)\\.(?<patch>\\d+)\\-RELEASE$",
"allowedVersions": "/^[0-9]+\\.[0-9]+\\.[0-9]+\\-RELEASE$/"
}
],
"customManagers": [
{
"customType": "regex",
"fileMatch": [
"^\\.gitmodules$"
],
"matchStrings": [
"\\[submodule \"(?<depName>.+)\"\\][^\\[]*?\\n\\s*branch\\s*=\\s*(?<currentValue>.*)"
],
"datasourceTemplate": "github-tags",
"versioningTemplate": "{{#if versioning}}{{versioning}}{{else}}semver{{/if}}"
}
],
"postUpgradeTasks": {
"commands": [
"(test \"{{{manager}}}\" = \"regex\" && git submodule update && git submodule init \"$(git config --file \".gitmodules\" submodule.{{{depName}}}.path)\" && git submodule update \"$(git config --file \".gitmodules\" submodule.{{{depName}}}.path)\" && (cd \"$(git config --file \".gitmodules\" submodule.{{{depName}}}.path)\" && git fetch --tags && git checkout \"{{{newVersion}}}\")) || true"
]
}
}
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:recommended"
],
"timezone": "Asia/Tokyo",
"git-submodules": {
"enabled": true,
"versioning": "git",
"fileMatch": [
"(^|/)\\.gitmodules$"
]
},
"packageRules": [
{
"matchManagers": [
"git-submodules"
],
"enabled": false
},
{
"matchDepNames": [
"jedisct1/libsodium"
],
"matchManagers": [
"regex"
],
"versioning": "regex:^(?<major>\\d+)\\.(?<minor>\\d+)\\.(?<patch>\\d+)\\-RELEASE$",
"allowedVersions": "/^[0-9]+\\.[0-9]+\\.[0-9]+\\-RELEASE$/"
}
],
"customManagers": [
{
"customType": "regex",
"fileMatch": [
"^\\.gitmodules$"
],
"matchStrings": [
"\\[submodule \"(?<depName>.+)\"\\][^\\[]*?\\n\\s*branch\\s*=\\s*(?<currentValue>.*)"
],
"datasourceTemplate": "github-tags",
"versioningTemplate": "{{#if versioning}}{{versioning}}{{else}}semver{{/if}}"
}
],
"postUpgradeTasks": {
"commands": [
"(test \"{{{manager}}}\" = \"regex\" && git submodule update && git submodule init \"$(git config --file \".gitmodules\" submodule.{{{depName}}}.path)\" && git submodule update \"$(git config --file \".gitmodules\" submodule.{{{depName}}}.path)\" && (cd \"$(git config --file \".gitmodules\" submodule.{{{depName}}}.path)\" && git fetch --tags && git checkout \"{{{newVersion}}}\")) || true"
]
}
}
43 changes: 4 additions & 39 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ jobs:
strategy:
matrix:
arch: ["amd64", "arm64v8", "s390x"]
version: ["8.1", "8.2", "8.3"]
version: ["8.2", "8.3"]
type: ["cli", "zts"]
distro: ["bookworm", "alpine"]
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: true
- name: Setup QEMU
uses: docker/setup-qemu-action@v3
with:
Expand All @@ -20,7 +22,7 @@ jobs:
uses: docker/setup-buildx-action@v3
- name: Build container
run: |
docker compose build --pull --no-cache --build-arg IMAGE=${{ matrix.arch }}/php --build-arg TAG=${{ matrix.version }}-${{ matrix.type }}-${{ matrix.distro }} --build-arg PSKEL_SKIP_DEBUG=${{ matrix.arch != 'amd64' && '1' || '' }}
docker compose build --pull --no-cache --build-arg IMAGE=${{ matrix.arch }}/php --build-arg TAG=${{ matrix.version }}-${{ matrix.type }}-${{ matrix.distro }} --build-arg PSKEL_SKIP_DEBUG=${{ matrix.arch != 'amd64' && '1' || '' }}
- name: Run tests
run: |
docker compose run --rm --entrypoint=/usr/bin/pskel_test --env TEST_EXTENSION=1 dev
Expand All @@ -44,40 +46,3 @@ jobs:
if: matrix.arch == 'amd64' && matrix.distro != 'alpine'
run: |
docker compose run --rm --entrypoint=/usr/bin/pskel_test --env TEST_EXTENSION_UBSAN=1 dev
# Windows:
# runs-on: windows-2022
# defaults:
# run:
# shell: cmd
# strategy:
# matrix:
# arch: ["x64"]
# version: ["8.1", "8.2", "8.3"]
# ts: ["nts", "ts"]
# steps:
# - name: Checkout
# uses: actions/checkout@v4
# - name: Setup PHP
# id: setup-php
# uses: php/[email protected]
# with:
# arch: ${{ matrix.arch }}
# version: ${{ matrix.version }}
# ts: ${{ matrix.ts }}
# - name: Enable developer command prompt
# uses: ilammy/msvc-dev-cmd@v1
# with:
# arch: ${{ matrix.arch }}
# toolset: ${{ steps.setup-php.outputs.toolset }}
# - name: phpize
# working-directory: ext
# run: phpize
# - name: configure
# working-directory: ext
# run: configure --enable-skeleton --with-prefix=${{ steps.setup-php.outputs.prefix }}
# - name: make
# working-directory: ext
# run: nmake
# - name: test
# working-directory: ext
# run: nmake test TESTS="--show-diff tests"
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "jedisct1/libsodium"]
path = ext/third_party/libsodium
url = https://github.com/jedisct1/libsodium.git
branch = 1.0.20-RELEASE
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,12 @@ WORKDIR "/usr/src/php"
COPY ./pskel_test.sh /usr/bin/pskel_test
COPY ./pskel_init_extension.sh /usr/bin/pskel_init_extension

# Install libsodium
COPY ./ext/third_party/libsodium "/ext/third_party/libsodium"
RUN cd "/ext/third_party/libsodium" \
&& ./configure \
&& make -j"$(nproc)" \
&& make install \
&& cd -

COPY ./ext /ext
54 changes: 12 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,23 @@
# Pskel
# extension_example

A skeleton project for quickly setting up an environment to develop extensions for PHP.
[`zeriyoshi/pskel`](https://github.com/zeriyoshi/pskel) を用いた PHP Extension のサンプルです。

### How to use
PHP 8.2 から利用できる `ext-random` ビルトイン拡張機能に [`libsodium`](https://github.com/jedisct1/libsodium) を用いた乱数生成器を追加するサンプル実装を含んでいます。

1. Install [Visual Studio Code](https://code.visualstudio.com/) and Docker Desktop (or an alternative engine).
1. Install the [Remote Container](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) extension in VSCode.
1. Open the directory and open it with the `Remote Container` extension.
1. Run `pskel_init_extension` command.
2. Run `pskel_test` command for testing.

### Q&A

#### Can I set up an environment using MySQL, Redis, etc.?

Yes. Pskel comes pre-setup with MySQL as a example. If you want to add something, you can easily do so by editing the `compose.yaml`.

#### Can I use a debug version of PHP?

A debug build of PHP is included in advance. Debug builds using GCC and Clang are available, and Valgrind support is enabled. With the Clang build, you can also use MemorySanitizer.

They each have the following binary prefixes. The build toolchains are the same.

- `debug-php`
- `gcc-valgrind-php`
- `clang-msan-php`
- `clang-asan-php`
- `clang-ubsan-php`

For example, the method to test the extension using GCC + Valgrind is as follows:

```
# gcc-valgrind-phpize
# ./configure --with-php-config=$(which gcc-valgrind-php-config)
# TEST_PHP_ARGS="-q -m --show-diff" make -j$(nproc) test
```

#### Can I debug using gdb?

Yes. Build using the debug version of PHP and run as follows:
### Usage

```
# gdb --args gcc-valgrind-php -dextension=./modules/your_extension_name.so example.php
$ git clone --recursive "https://github.com/colopl/php-extension_example" "extension_example"
$ cd "extension_example/ext"
$ phpize && ./configure --with-php-config="$(which php-config)"
$ make -j"$(nproc)" && make test
$ [sudo] make install
```

#### Can I develop using something other than Visual Studio Code?
### Development

While it's not recommended, it's possible. You can using Docker Compose (or a alternative engine).
このプロジェクトは [`zeriyoshi/pskel`](https://github.com/zeriyoshi/pskel) をベースに作成されています。基本的な使い方は Pskel と同様です。

### License
### LICENSE

PHP License 3.01
2 changes: 0 additions & 2 deletions docker/mysql/etc/mysql/conf.d/my.cnf

This file was deleted.

44 changes: 44 additions & 0 deletions ext/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
*.lo
*.la
*.dep
.libs
acinclude.m4
aclocal.m4
autom4te.cache
build
config.guess
config.h
config.h.in
config.h.in~
config.log
config.nice
config.status
config.sub
configure
configure~
configure.ac
configure.in
include
install-sh
libtool
ltmain.sh
Makefile
Makefile.fragments
Makefile.global
Makefile.objects
missing
mkinstalldirs
modules
php_test_results_*.txt
phpt.*
run-test-info.php
run-tests.php
tests/**/*.diff
tests/**/*.out
tests/**/*.php
tests/**/*.exp
tests/**/*.log
tests/**/*.sh
tests/**/*.db
tests/**/*.mem
tmp-php.ini
1 change: 0 additions & 1 deletion ext/.gitkeep

This file was deleted.

22 changes: 22 additions & 0 deletions ext/config.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
dnl config.m4 for extension_example extension

PHP_ARG_WITH([extension_example], [for extension_example support], [AS_HELP_STRING([--with-extension_example]), [Include extension_example support]])

if test "${PHP_EXTENSION_EXAMPLE}" != "no"; then
dnl check for required libraries and headers
AC_CHECK_LIB([sodium], [sodium_version_string], [], [
AC_MSG_ERROR([Required library sodium not found. Please install libsodium library])
])

AC_CHECK_HEADER([sodium.h], [], [
AC_MSG_ERROR([Required header sodium.h not found. Please install libsodium library])
])

PHP_ADD_INCLUDE("/usr/local/include")
PHP_ADD_LIBRARY_WITH_PATH(sodium, "/usr/local/lib", EXTENSION_EXAMPLE_SHARED_LIBADD)

PHP_SUBST(EXTENSION_EXAMPLE_SHARED_LIBADD)
PHP_SUBST(EXTENSION_EXAMPLE_LIBS)

PHP_NEW_EXTENSION(extension_example, extension_example.c sodium.c, $ext_shared)
fi
Loading

0 comments on commit ade1dc2

Please sign in to comment.