Discussing the nuts and bolts of software development

Wednesday, July 30, 2008

 

Snake On A Phone

For a few years now, my main task at work has been working on the firmware of an IP phone. The phone runs VxWorks on a MIPS32 CPU; the firmware is written in C and C++.

For slightly less time, I've been dabbling in python on my own time. Freedom from explicit typing was a refreshing change, and python's tendency to Just Work was a nice bonus.

It was perhaps inevitable that I would one day try to combine phone and language. (why? because they were both there)

It wasn't obvious that the idea stood a chance. VxWorks is a bit off the OS beaten track, and might not provide all the functionality needed by Python's "core" (not with the same names, anyway). There might be some processor-specific pieces that would rule out MIPS32. And even if I could get something built, would it fit in the 2 or 3 MB of RAM (and even less flash) I could spare?

As it turns out, there was very little to worry about. Python's code is impressively (if perhaps unsurprisingly) portable, only needing a couple of tweaks to its build system and none at all to its source code. There doesn't seem to be anything CPU-dependant; and in the end, adding python to my firmware only cost me 1MB. It took me only a few evenings of tinkering to get a libpython built, linked into my firmware, and loaded on my phone, to the point that I could run this little experiment at the VxWorks shell:


-> Py_Initialize()
value = 42 = 0x2a = '*'
-> PyRun_SimpleString("print 'Hello, World!'\n")
Hello, World!
value = 0 = 0x0


(the VxWorks shell being a peculiar animal that allows calling C functions by name, in this case giving me access to Python's C Extension API for a near-REPL experience)


For my purposes, that's enough; I know it can work, and that's all I wanted. I don't expect to ever go further than this. But as little as it is, publishing how I got there might help someone get started on a real project; so here goes:

Porting python in 10 easy* steps



*for a suitable definition of easy


  1. As far as embedding Python in an existing application (or firmware) is concerned, Python's own documentation should give you most of what you need
  2. You'll need a cross-compiling toolchain, i.e. a compiler that can be used on one platform (e.g. x86) and produces executables for use on a different platform (e.g. MIPS32). GCC is your best bet; it's what will make python's build system happiest, and there's lots of resources on getting a GCC cross-compiler working on the web, though it looks a bit daunting to me. I was fortunate in that, since I was already set up to build firmwares, I already had all the needed tools; I would guess that most people engaging on a similar project would be in the same position.

  3. In addition to the compiler (and assembler, linker, etc), you'll want to have a Unix-like environment to run Python's configure script and makefiles. If you're on Windows, cygwin will serve nicely.

  4. The 'configure' script needs some tweaking: it contains a few uses of AC_TRY_RUN, which will fail when cross-compiling.
    • If you have a working 'autoconf', the simplest is to edit the 'configure.in' file. You can either remove the AC_TRY_RUN tests altogether or replace them by the newer, more cross-compiler-friendly AC_RUN_IFELSE. Then run 'autoconf' to regenerate the 'configure' script.
    • If you don't (as I didn't), you can brace yourself and go edit the 'configure' script directly. Running the script produces error messages that gives something to search for. The fix is actually simple: just remove the calls to 'exit' to allow the error to get ignored.


  5. The makefile also needs tweaking: just like 'configure', at some point it tries to compile and run a program. This appears to be in order to autogenerate some source files, which fortunately are already provided in the source distribution; so it's safe to disable this step. The simplest way:
    • open "Makefile.pre.in"
    • find the place where "$(PGEN)" shows up AFTER a ':'
    • remove "$(PGEN)"

    (this will only prevent the executable from getting built. The makefile will still attempt to run it, but it's written so that the resulting failure is ignored)

  6. The configure script and makefile try to guess at the name of tools to use; you can give them a hint with environment variables. In my case I needed to set CC (the C Compiler) and AR (the "archiver", ie. what creates static libraries)

  7. If you need to specify special command-line options to the compiler, environment variables can also be used. Annoyingly, 'configure' and the makefiles use different variable; you'll want to set CFLAGS and BASECFLAGS to the same thing.

  8. Finally you'll be ready to run the 'configure' script. You need to give it the special options --build and --host to tell it you're cross-compiling, something like:

    $ ./configure --build=win32 --host=vxworks


    (win32 and vxworks were a wild guess that happened to work for me. I got the impression the specific values didn't particularly matter)

  9. You can then run 'make' to compile everything. If, like me, all you need is a static library, this will do it:

    $ make libpython2.5.a


  10. There's a good chance some files under Modules/ will fail to compile (in my case, posixmodule.c). The file Module/Setup specifies (in a rather well-documented way) which Python modules (written in C) should be built into the python library; comment out the failing one, and re-run 'make'. I only had to disable posixmodule and pwdmodule; YMMV.


And for me, that was it; nothing else needed manual intervention. If you run into more troubles (e.g. trying to build the actual python.exe), I'm afraid you're on your own.

My next step was to figure out how to integrate the python library into my firmware; you'll have to figure out the corresponding steps for your own firmware/embedded application/whatever. Start with the 'embedding' link for how to access python code from your code.

If you want to be able to load python source files with 'import', pay particular attention to what that page says about PYTHONHOME; as for me, I put a putenv(PYHONHOME=/whatever") before the Py_Initialize call, letting me import /whatever/python2.5/*.py files (and possibly, though I haven't tried, .py files contained in a /whatever/python2.5/libpython2.5.zip)

Happy cross-compiling!

Labels: , , ,


Comments:
dongtam
mu private
tim phong tro
http://nhatroso.com/
nhac san cuc manh
tổng đài tư vấn luật
http://dichvu.tuvanphapluattructuyen.com/
văn phòng luật
tổng đài tư vấn luật
dịch vụ thành lập công ty
http://we-cooking.com/
chém gió
trung tâm ngoại ngữ.

- Hừ.

Hoàng Vũ hừ lạnh một tiếng, thân thể sau đó lui lại rồi nói:

- Các ngươi cẩn thận.

- Xoẹt xoẹt.

Ba đạo thân ảnh chuyển động, ngay sau đó bốn thanh niên hán tử đánh về phía bốn người của Thần Hoàng Tộc, cường hãn công kích của bọn họ ngưng tụ.

- Ầm ầm.

Một luồng khí tức nổ ra, quang mang công kích chạm vào nhHoàng Quang, ngươi đi đối phó với hai nữ nhân kia trước đi.

Nhất tinh Đấu Thánh vừa bị Hoàng Vũ đánh liền cười nói.

- Xoẹt.

Lão nhân kia không hề do dự mà đánh về phía Thượng Quan Uyển Nhi và

 
Hey! You should check this out to find the best tips for a young writers!
 
IEEE Project Domain management in software engineering is distinct from traditional project deveopment in that software projects have a unique lifecycle process that requires multiple rounds of testing, updating, and faculty feedback. A IEEE Domain project Final Year Projects for CSE system development life cycle is essentially a phased project model that defines the organizational constraints of a large-scale systems project. The methods used in a IEEE DOmain Project systems development life cycle strategy Project Centers in Chennai For CSE provide clearly defined phases of work to plan, design, test, deploy, and maintain information systems.


This is enough for me. I want to write software that anyone can use, and virtually everyone who has an internet connected device with a screen can use apps written in JavaScript. JavaScript Training in Chennai JavaScript was used for little more than mouse hover animations and little calculations to make static websites feel more interactive. Let’s assume 90% of all websites using JavaScript use it in a trivial way. That still leaves 150 million substantial JavaScript Training in Chennai JavaScript applications.
 
Thank You SO Much For This Lovely Post. By The Way You Can Check Out Quickbooks Support If You face Any Problem Regarding Quickbooks Software or Application.

 
Snake on a phone is the game played by most of the people around 8-10 years back. It was created on Java for Symbian phones. Whereas, it was also developed by the game developers in python for smartphone users. It was fun playing that game. My son still plays that game on my phone when I get busy in my essay help online.
 
The information above is good here is viewmypaycheck for all the tax related query to check your tax slab just visit the link viewmypaycheck
 

Thanks for your post!


شركة شحن عفش من جدة الى الامارات شركة شحن عفش من جدة الى الامارات
شركة شحن عفش من جدة الى الاردن شركة شحن عفش من جدة الى الاردن


 
Try to create a paper about something that you are aware of. Thus, you will not encounter any serious difficulties during the writing process. Do not pick any complex subjects and
buy 3000 word essay.
 
Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work.
Renegade Racing For PC
 
Quickbooks is one of the advanced accounting software that offers multiple accounting features to manage your business data conveniently. This software allows users to create a backup file and restore the data. With quickbooks data transfer, you can easily and quickly send the data from one pc to another.
 
QuickBooks tool applications is one of the best application that can be used in removal of errors from Quickbooks. It can remove most of the error that might occur in Quickbooks very easily and with minimum time.
 
Quickbooks update improve the performance of the software .
Also,make it more productive and fix the common and complicated errors automatically.
You must update Quickbooks version whenever a new feature is released.
for more information on Quickbooks update visit our site
https://updateerror.co/update-quickbooks/

 
Quickbooks Error Support solves all Quickbooks errors related problems, so if you are facing any problems relating to QB errors, head to QB errors for technical assistance.

 

Thank you so much for sharing all this wonderful information !!!! It is so appreciated!! You have good humor in your blogs. So much helpful and easy to read!
Python training in Pune

 
Thanks for sharing this with your audience and Python geeks, Visit us for Python Training in Pune
 
This blog is what I was looking for. This piece of content will really help me. Thanks for sharing it.polymer door Suppliers
 
I recently came across your article and have been reading along. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end.
Python Classes in Pune



 
When you access your QuickBooks company files or the database connection verification fails, the quickbooks error 6129 is likely to appear on your screen. As a result, you are unable to access your company's files in QuickBooks. Furthermore, the network that your QuickBooks is using will be unable to locate the server or your device that stores the file.
 
You can contact us at Write an Essay For Me in any part of the world as we deliver to native and non-native speakers of English language alike.
 
Quickbooks error 15106 is the most common symptom of this issue. With its numerous significant and beneficial features, this software has never failed to astound its users. However, QB tool has various limitations and restrictions, one of which being QB error 15106. There is no such thing as an unsolvable problem with QuickBooks, and Error 15106 is one of them.
 
quickbooks error 15227 will occur when the workstation shuts down randomly and when the user switches off or turns on or when installing or updating QuickBooks.
 
Quickbooks is accounting software. Generally, QuickBooks Error 15227 appears if your computer is unstable or essential files do not respond to your commands or go missing.
 
QuickBooks is often regarded as error-free software, but there are a few glitches that can cause problems for users. QuickBooks database server manager network diagnostics error can appear on the computer screen when upgrading and running QuickBooks in multi-user mode. When accessing the company register, users can experience errors such as Error H202 and other network connection errors. If you’ve made a similar error, don’t worry; we’ve got you covered with this post. The user can find it difficult to deal with QBDBSM network diagnostics errors.
 
A debt of gratitude is in information for Sharing data this data is extremely useful and helpful for me and different people. I like your work.


 
This article is an appealing wealth of useful informative that is interesting and well-written. I commend your hard work on this and thank you for this information.

This article is an appealing wealth of useful informative that is interesting and well-written. I commend your hard work on this and thank you for this information.
 
Fast-growing consumer demand driven by the advent of digital solutions led to the replacement of critical machines with technology-driven smart solutions. Now, the US as well as the world is witnessing the next stage of such intelligent solutions in the form of Automation!

More than a trend, Automation has become a need of the hour for many firms across the global countries including the US.

Many firms are looking at it as a critical driver of trending technology solutions they deploy on board.

One such technology trend that gave due attention to Automation is DevOps, which eventually became popular as ‘DevOps Automation’.
 
Thanks for sharing this knowledgeable post. What an excellent post and outstanding article. Thanks for your awesome topic. Really I got very valuable information here. If you want to fix Roadrunner Email Login Problems please contact roadrunner support team for solution.
 
Are you looking for Mobile app development company. Appcul will be the best choice for you. Here we provide different type of technology service in mobile and web applications. Also our budget range is that you can easily afford.

 
This comment has been removed by the author.
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?