Skip to content

Add rule python.cython #6061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 16, 2025
Merged

Add rule python.cython #6061

merged 3 commits into from
May 16, 2025

Conversation

Freed-Wu
Copy link
Member

@Freed-Wu Freed-Wu commented Jan 12, 2025

  • Before adding new features and new modules, please go to issues to submit the relevant feature description first.
  • Write good commit messages and use the same coding conventions as the rest of the project.
  • Please commit code to dev branch and we will merge into master branch in feature
  • Ensure your edited codes with four spaces instead of TAB.

  • 增加新特性和新模块之前,请先到issues提交相关特性说明,经过讨论评估确认后,再进行相应的代码提交,避免做无用工作。
  • 编写友好可读的提交信息,并使用与工程代码相同的代码规范,代码请用4个空格字符代替tab缩进。
  • 请提交代码到dev分支,如果通过,我们会在特定时间合并到master分支上。
  • 为了规范化提交日志的格式,commit消息,不要用中文,请用英文描述。

Future work:

  • rule python.module to set the correct extension for python module.
  • rule python.autopxd to convert *.h to *.pxd
  • allow rule python.cython to output C++
  • allow rule python.cython to use -I for *.pxd

cython use a header file with extension pxd. Should we use same add_includedirs() for it?
That is, if we

add_includedirs(".")

We will

cython -3omain.c -I. main.py

@waruqi waruqi closed this Jan 13, 2025
@waruqi waruqi reopened this Jan 13, 2025
@Freed-Wu Freed-Wu force-pushed the cython branch 3 times, most recently from 136c39d to fd949da Compare January 14, 2025 12:44
@Freed-Wu
Copy link
Member Author

Freed-Wu commented Jan 14, 2025

print(1)
$ cython -3omain.c main.py
$ cythonize -b main.c
$ python -c 'import main'
1

For xmake:

add_rules("mode.debug", "mode.release")
add_requires("python 3.x")

rule("python.cython")
    set_extensions(".py", ".pyx")
    before_buildcmd_file(function(target, batchcmds, sourcefile, opt)
        local language = target:extraconf("rules", "python.cython", "language")
        local ext
        local arg = "-3"
        if language == "c" then
            ext = "c"
            local rule = target:rule("python.cython"):clone()
            rule:add("deps", "c", {order = true})
            target:rule_add(rule)
        elseif language == "c++" then
            ext = "cc"
            arg = arg .. "+"
            local rule = target:rule("python.cython"):clone()
            rule:add("deps", "c++", {order = true})
            target:rule_add(rule)
        end
        local dirname = path.join(target:autogendir(), "rules", "python", "cython")
        local sourcefile_c = path.join(dirname, path.basename(sourcefile) .. "." .. ext)

        -- add objectfile
        local objectfile = target:objectfile(sourcefile_c)
        table.insert(target:objectfiles(), objectfile)

        -- add commands
        batchcmds:show_progress(opt.progress, "${color.build.object}compiling.python %s", sourcefile)
        batchcmds:mkdir(path.directory(sourcefile_c))
        import("lib.detect.find_tool")
        local cython = find_tool("cython")
        assert(cython, "cython not found! please `pip install cython`.")
        batchcmds:vrunv(cython.program,
            { arg, "-o", path(sourcefile_c), path(sourcefile) })
        batchcmds:compile(sourcefile_c, objectfile)

        -- add deps
        batchcmds:add_depfiles(sourcefile)
        batchcmds:set_depmtime(os.mtime(objectfile))
        batchcmds:set_depcache(target:dependfile(objectfile))
    end)

target("main")
add_rules("python.library", "python.cython", { soabi = true, language = "c++" })
add_files("src/*.py")
add_packages("python")
$ xmake --rebuild --verbose
[ 50%]: compiling.python src/main.py
/usr/bin/cython -3+ -o build/.gens/shm/linux/x86_64/release/rules/python/cython/main.cc src/main.py
/usr/bin/gcc -c -m64 -fPIC -fvisibility=hidden -fvisibility-inlines-hidden -O3 -isystem /usr/include/python3.13 -DNDEBUG -o build/.objs/shm/linux/x86_64/release/gens/rules/python/cython/main.cc.o build/.gens/shm/linux/x86_64/release/rules/python/cython/main.cc
checking for g++ ... /usr/bin/g++
checking for the shared library linker (sh) ... g++
checking for flags (-fPIC) ... ok
[ 75%]: linking.release shm.cpython-313-x86_64-linux-gnu.so
/usr/bin/g++ -o build/linux/x86_64/release/shm.cpython-313-x86_64-linux-gnu.so build/.objs/shm/linux/x86_64/release/gens/rules/python/cython/main.cc.o -shared -m64 -fPIC
[100%]: build ok, spent 0.804s
$ cd build/linux/x86_64/release/
$ python -c 'import main'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import main
ImportError: dynamic module does not define module export function (PyInit_main)

@Freed-Wu
Copy link
Member Author

python.library should be renamed to python.module, same as nodejs.module, lua.module, platform.linux.module, etc.

@waruqi
Copy link
Member

waruqi commented Jan 14, 2025

please add some examples in tests

@Freed-Wu Freed-Wu force-pushed the cython branch 2 times, most recently from fd37bd5 to 22e9730 Compare January 15, 2025 16:15
@waruqi
Copy link
Member

waruqi commented Jan 22, 2025

@Freed-Wu Freed-Wu force-pushed the cython branch 3 times, most recently from f9978ce to abdaa45 Compare May 14, 2025 11:39
@Freed-Wu Freed-Wu marked this pull request as draft May 14, 2025 11:55
@Freed-Wu Freed-Wu marked this pull request as ready for review May 14, 2025 12:12
@Freed-Wu
Copy link
Member Author

Freed-Wu commented May 14, 2025

add_deps("python.library", {soabi = true} )

cannot work.

And why not set soabit's default value is true? a correct python module must set soabi to be recognized correctly.

@waruqi waruqi merged commit 9375cff into xmake-io:dev May 16, 2025
22 checks passed
@waruqi waruqi added this to the v3.0.0 milestone May 16, 2025
@Freed-Wu Freed-Wu deleted the cython branch May 16, 2025 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants