Wednesday, December 24, 2008

Installing GCC on Eee PC: A Walkthrough

This is a step-by-step walkthrough of how to build and install latest GCC (4.3.2 at the time of writing) from source on Eee PC 900 model with pre-installed Linux. The steps for other models may be similar but there's no way I can test it now.

0. Why do I need a GCC?

Well, why not? I cannot bear the fact that Eee PC does not come with a pre-installed gcc, since this basically says, hey, we don't want you to build your own stuff on it. What a thing to do, considering that they have already provided the assembler (as), linker (ld), pre-processor (cpp), text editor (vim), GNU make, Perl, Python, and many more!

The system itself is Xandros based. No problem. But it's the stable release. Err, can't they take a little risk and provide more up-to-date software? Firefox is of version 2.0.0.14 after upgrading, for example, and "Check for Update" is disabled within firefox.

I may be able to use it to check email for now, but I felt the urge to take control. So here's what I did as a first step to extend the system.

1. Preparations

1.1 Getting a shell

I am a gnome user, so the user interface looks a bit like maze to me. The first thing I tried in my local retailer was to launch a console program. It took me a good 10 minutes to find a way. Basically you could open a file manager and press Ctrl-T, which would give you a full fledged Konsole. But I later learned that there's an easier way: Just press Ctrl-Alt-T on the main desktop interface and you will get an xterm.

1.2 Preparing directory

My 900 model comes with a claimed 20G solid state disk. It was divided into two partitions, one of them 3.7G mounted as / and and the other 14.7G mounted as /home. The first partition, being used mainly by the pre-installed system, already took most of the space on it. So it's not a good idea to directly install anything there. However, installing everything to user home directory is inconvenient, since you need to modify default prefix when building, and makes installation of binary packages more difficult.

My solution is to move the entire /usr/local/ in to /home, and create a symbolic link there to refer to the one in /home. The commands to achieve this are:

sudo mv /usr/local /home
sudo ln -s /home/local /usr/


1.3 Getting a binary gcc package

The fact that Eee PC does not come with a compiler makes things a bit difficult to bootstrap. To compile GCC, you need GCC :)

The solution is, naturally, to get a minimum binary package from somewhere first. I first thought Debian packages would work, but I was wrong. Debian packages are tagged with an extra string after the normal package name and version number to identify which release it belongs to. This creates dependency problems. After some googling I found the following method to get gcc-4.1.2 from a xandros repository.

First, add the following line in /etc/apt/sources.list

deb http://xnv4.xandros.com/xs2.0/pkg/ xs2.0-xn main

Next, update the list of available packages by

sudo apt-get update
sudo apt-get install xandros-archive-keyring


The second command is just to let the system accept the public key of the xandros repository. After that, install gcc and libc6-dev as follows

sudo apt-get install gcc
sudo apt-get install libc6-dev


These may prompt you to install extra packages, such as linux-headers, but that's fine, and it won't take too much space. After this I got a working C compiler.

2. Building GCC

A detailed instruction of how to build GCC from source can be found at http://gcc.gnu.org/install/. However, many of the steps and options are not relevant here. So I'm going to zoom in to those things that are not the default.

2.1 Download the source

Three packages will be needed. First is GCC itself, then GMP, and lastly MPFR. Links can all be found from the gcc instruction page. There are two things to note here. One is that you only need the source of all three packages. GMP and MPFR will be built together with GCC. Second, if you are not so keen to compile Ada and Fortran etc. using gcc, you can just get gcc-core and gcc-g++ packages, which will be about only half of the size of the full version.

After downloading the source packages, decompress them in the same directory. I used /home/user/sw/. Each of these packages will have their own sub directory (except for gcc-g++ and gcc-core, they will go into the same directory).

2.2 Building from source

Before you actually start to build, you need to tweak the directories a bit. Suppose you are in /home/user/sw/ (replace this by wherever you decompress the source files). Also, suppose you downloaded gcc-4.3.2, gmp-4.2.4 and mpfr-2.3.2. You can do the following.

mkdir build
cd gcc-4.3.2
ln -s ../gmp-4.2.4 gmp
ln -s ../mpfr-2.3.2 mpfr


After that you can start to configure GCC in the "build" directory as created above (building inside the source directory is not recommended as stated on the gcc webpage).

cd /home/user/sw/build
../gcc-4.3.2/configure


By default the installation directory prefix is /usr/local/, which can be changed by "--prefix=dir" parameter to the configure script. After that you can start to build and install it.

make
sudo make install


Verify that you are using the new gcc by "gcc --version". I had to exit the current xterm and launch a new one to do that because although /usr/local/bin is before everything else in $PATH, somehow when I run gcc it still runs the old version in the existing xterm window.

Thursday, December 18, 2008

When VMware meets Ubuntu

VMware is a cool thing. Only after you have a few gigs of RAM and dual/quad-core, that is. Although it is a proprietary technology, it does have a free server version that is tremendously helpful to me, both in daily programming work and casual time-killing.

Ubuntu is another cool thing, which I enjoy using at the moment. But it has certain ``problems'' (or maybe ``features'' that I'm not used to). One of them pops up when I use VMware server 2.0 on Ubuntu 8.10.

In short, everything went fine (well, almost), except that I found my UP arrow key is gone, and my DOWN arrow key becomes the Windows key in my guest XP.

This was not a new problem. I had the same problem with previous versions of VMware and Ubuntu, but unfortunately forgot what the solution was. I had a one-line script that reads:

setxkbmap -rules xorg -layout "us" -model pc

which I remember solved one of the previous problems, but it didn't work anymore.

Googling on VMware and Ubuntu gives me a lot of information, so much so that I couldn't find relevant solution easily. Eventually I came across this howto:

How to VMWare Server 2.0 on Ubuntu 8.10

This basically says that adding the following line in /etc/vmware/config would solve the problem for VMware server (VMware Workstation requires a bit more work).

xkeymap.nokeycodeMap = TRUE

This one-line solution solved my keyboard problem. I'm really glad that someone took the trouble to write this howto to help people like me, who have problems remembering every detail.

Monday, December 15, 2008

Fine Tuning Locale in Ubuntu GNU/Linux

http://isis.poly.edu/~qiming/fine-tuning-ubuntu-locale.html

0. Background

A few years ago, I was quite frustrated in configuring locale settings in Debian when I switched from RedHat (not that it was any easier for RedHat), so much so that I ended up writing my own mini howto on it, hoping it would be useful to folks like me.

I also tried Gentoo for a couple of years, but gave up the idea of compiling everything from the source and the long process of installation (I could have used the binary packages but that would sort of defeat the purpose, wouldn't it?). Configuring locale in Gentoo was quite straight forward, though.

Recently I lean more on Ubuntu, mainly for its effortless installation and configuration processes. Although I lost a little of control as what should and should not be installed on my system, but with cheap hard drives and CPUs today, that seemed to be a reasonable trade-off.

1. Locale configuration problems in Ubuntu

Locale configuration in Ubuntu was, as in Gentoo, quite straight forward, provided that you use the default gdm, since it allows you to specify a locale at the login window, and Gnome handles locales pretty nicely. For example, scim would be run (and working nicely) when I chose "zh_CN.UTF-8" as my locale. It should be pretty much the same for kdm and KDE but I didn't try.

However, a couple of little tricks I used to play with Debian wouldn't work in Ubuntu. For example, in Debian I could have set the locale to "C" and still have scim working on my browser and gnome-terminal, whereas in Ubuntu scim simply wouldn't work that way.

Also, when I set the locale to zh_CN, I lost control of menu displays and error messages. Although it wouldn't hurt to read error messages in Chinese, it sometimes requires a bit of guess work to decipher what they really mean, especially those errors and warnings emitted from gcc.

Ocasionally you would need to copy/paste the messages to your browser to google about a solution to your problem, and error messages in Chinese (which is not standardized in anyway) just wouldn't help.

Most importantly, when you want to view man pages, having a locale other than "C" could make it complain and refuse to work. Same goes for programs like aspell.

In Debian I could put something in /etc/X11/Xsession.d/ to fine tune the locale settings (as I explained in the mini howto) But it didn't seem to work in Ubuntu (at last for for some release versions that I tried). In some cases I could work it around using the enstart.sh script as in the mini howto, but it's troublesome.

2. Fine Tuning

In the end, I figured out that /etc/environment is the right place to place the locale settings. Basically I inserted the following lines into the file but leave "LANG" and "LANGUAGE" unchanged:

LC_MESSAGES="C"
LC_COLLATE="C"
LC_CTYPE="C"
LC_TIME="C"
LC_NUMERIC="C"
LC_MONETARY="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT="C"
LC_IDENTIFICATION="C"

(EDIT: Actually, only LC_MESSAGES is required in most cases. Putting all of them here may confuse Ubuntu as I recently found out.)

After that, log out of Gnome (which seems to have the side effect of restarting gdm, otherwise I may have to restart it manually), and log in back again, and menu items, error messages, man pages, etc., would be displayed in English, without affecting scim.

A minor (but surprising!) problem is that the /etc/environment file does not support usual variable substitution as in a shell script. So I cannot put something like

ENCODING="C"
LC_MESSAGES=$ENCODING
...

as I did in the mini howto. What happens to the Least Surprise priciple, I wonder.

Sunday, April 16, 2006

Chinese Debian Mini Howto

http://isis.poly.edu/~qiming/chinese-debian-mini-howto.html

0. Introduction
The purpose of this mini howto is to help users of Debian GNU/Linux to build a minimum Chinese environment, so that they can read and input Chinese in their systems.

To support Chinese language display and input under Debian GNU/Linux, you will need to do the following basic steps.
*Generate relevant locales
*Install Chinese fonts
*Install an input method (IM) engine
*Set locale
*Adjust application settings (if necessary)

In the rest of this text, I will explain these steps one by one. Most of the commands below need to be run in a terminal with root privilege.

1. Generating Locales
Run dpkg-reconfigure locales, and choose the following items.
*en_US ISO-8859-1
*zh_CN GB2312
*zh_CN.GBK GBK
*zh_CN.UTF-8 UTF-8
*zh_TW BIG5
*zh_TW.UTF-8 UTF-8
Some of these are optional. For example, if you are using Simplified Chinese only, you would not need the last two items. After this you will be prompted for the default locale you want to use.

NOTE: Sometimes you need to reboot to get the new locales working. To avoid potential problems, it is strongly recommended that you get this correct from the beginning when you install your system.

2. Installing Chinese Fonts
Install at least the following free fonts. Each entry below is of the form package_name (font_name).
*ttf-arphic-gbsn00lp (AR PL SungtiL GB)
*ttf-arphic-gkai00mp (AR PL KaitiM GB)
*ttf-arphic-bsmi00lp (AR PL Mingti2L Big5)
*ttf-arphic-bkai00mp (AR PL KaitiM Big5)
The first two are for Simplified Chinese, and the other two for Traditional Chinese.

These packages can be installed by running
apt-get install ttf-arphic-bkai00mp ttf-arphic-bsmi00lp ttf-arphic-gbsn00lp ttf-arphic-gbsn00lp

There are other fonts available. You can find them by searching for "xfonts" using dselect.

3. Installing Input Method (IM) Engine(s)
You will need an IM engine to input Chinese characters under X. There are a few IMs around, including xcin, chinput, scim, etc. Personally I found scim a good tool.

There are several packages related to scim. The easiest way to install it is by running
apt-get install scim scim-chinese scim-tables-zh
The package names may change in the future (as they did in the past). In that case, you can search for packages beginning with "scim" using dselect, examine their descriptions, and choose the input methods you need. After that dselect will do the rest of the job by selecting all dependent packages.

After that, create a new file /etc/X11/Xsession.d/95xinput with the following content.

/usr/bin/scim -d
XMODIFIERS="@im=SCIM"
export XMODIFIERS

This script will be run every time X windows starts. In case you want to be more flexible, you can put something more complicated in the file. For example,

case "$LANG" in
zh_TW*)
/usr/bin/scim -d
XMODIFIERS="@im=SCIM"
;;
zh_CN*)
/usr/bin/scim -d
XMODIFIERS="@im=SCIM"
;;
esac
export XMODIFIERS

This takes effect only when you restart X. The simplest way to do that is to press "Ctrl-Alt-BackSpace".

To use scim, simply press "Ctrl-Space", and a small window will appear at the lower right corner of the desktop.

It is advisable that you configure scim (right click on its icon on the panel, then configure) and remove all unwanted input methods. You will need to restart scim (probably X, too) to make this to take effect.

4. Setting Locale
It is highly recommended that you use gdm or kdm as your X display manager, because then you will be able to select your language settings at the login window, which can be different from system default, and can be different for different login.

If you are using a X display manager that does not support this, you will have to put an additional line such as
export LANG=zh_CN.gb2312
in /etc/X11/Xsession.d/95xinput.
NOTE: It would not work if you run this after you login. You will need to restart X for this to work.

REMARK: One "side effect" of this is that once you set the language to be Chinese, then all the menu become Chinese. For those who want to keep the English menu but still want to view/input Chinese, you can set locale to be zh_CN.gb2312, but change the settings for some environment variables. For example, I have the following lines in the above file.

ENCODING="en_US"
#export LC_ALL=$ENCODING
export LC_MESSAGES=$ENCODING
#export LC_COLLATE=$ENCODING
#export LC_CTYPE=$ENCODING
export LC_TIME=$ENCODING
export LC_NUMERIC=$ENCODING
#export LC_MONETARY=$ENCODING
#export LC_PAPER=$ENCODING
#export LC_NAME=$ENCODING
export LC_ADDRESS=$ENCODING
export LC_TELEPHONE=$ENCODING
export LC_MEASUREMENT=$ENCODING
export LC_IDENTIFICATION=$ENCODING

Then I got English display of menu, time and date, etc. You should comment/uncomment these items according to your needs.

Note that "scim" works fine no matter what locale you choose.

5. Application Settings
5.1 Web Browsers
For applications such as Mozilla (1.7) and/or other browsers, usually you do not have to change much. If a Chinese webpage does not display correctly, try to check if the character encoding is correct, and make sure that you have installed the corresponding fonts.

For Mozilla version 1.6, there are some locale packages such as mozilla-locale-zh-cn or mozilla-locale-zh-tw.

For Mozilla-Firefox, you might need to install one of the mozilla-firefox-locale-zh-cn or mozilla-firefox-locale-zh-tw packages.

5.2 Editors
My favorite text editor is VIM with GTK support, or simply gvim. For gvim to display Chinese characters correctly, just add the following lines to $HOME/.gvimrc.

set enc=euc-cn
set tenc=euc-cn
set fileencoding=euc-cn
set guifont=AR\ PL\ KaitiM\ GB\ 12

The last line specifies the font and the font size to use. You can change it to any of the four fonts as in Section 2 above, and adjust the font size until you feel comfortable.

Note that even when you set LC_MESSAGE to en_US, gvim might still display Chinese menu if your locale is set to Chinese. In this case, you need a bit trick here.

Firstly, you need to create a file with the content something like the following.

#!/bin/sh
# Start application $1 with English environment

if [ -z "$1" ]; then
echo "Usage: $0 app arg1 arg2 ... "
exit 1
fi

export LANG=en_US

PROG=$1
shift
exec $PROG $*

Let us call this file enstart.sh, and put the file in a directory that is in your $PATH, e.g., /usr/local/bin. Make sure that it is executable by running chmod +x /usr/local/bin/chstart.sh in a terminal.

This small shell script will set $LANG so that the application it runs would think that it is running in a full English system.

To run gvim, we run enstart.sh gvim instead. You can add this to your desktop/panel shortcut, or make it an alias.

5.3 Terminal Emulators
RXVT (www.rxvt.org) is a nice terminal-emulator that intends to replace xterm.
It has a variant rxvt-ml which supports display of Chinese and Japanese characters.

The gnome-terminal, the default terminal emulator in the GNOME environment, supports Chinese by selecting the character encoding from the terminal menu.

The default terminal emulator in KDE, Konsole, works fine with Chinese automatically if the locale is set correctly. You can also select/change character encoding from the menu.

5.4 Display of Chinese File Names in FAT Partitions
You will need kernel support for this. To be able to mount a FAT (either 16 or 32 bit), you will need the following module.

fat
vfat

To display Chinese characters properly, you will need at least one of the following modules:

nls_cp936 (for simplified Chinese)
nls_cp950 (for traditional Chinese)
nls_utf8 (for Unicode characters)

Then in the file /etc/fstab, add another line like the following
/dev/hda5 /mnt/dos vfat noauto,user,codepage=936,iocharset=cp936 0 0
for simplified Chinese, and replace the number 936 to 950 for traditional Chinese, and to utf8 for Unicode characters.

NOTE: you should change the partition (/dev/hda5) and mount point (/mnt/dos) to the partition you want to mount, and the directory you want it to be mounted to, respectively.

5.5 XMMS
Unfortunately, unlike other programs, my XMMS does not display Chinese file/song names correctly, even when I set both the system locale and the language option of gdm to be zh_CN.gb2312, which is quite weird. My guess is that this has to do with the locale I set when I first install the system.

Fortunately, similar trick as in the case of gvim can be applied here, but this time we want to run with Chinese support. Let us create the following file and call it chstart.sh

#!/bin/sh
# Start application $1 with Chinese environment

if [ -z "$1" ]; then
echo "Usage: $0 app arg1 arg2 ... "
exit 1
fi

export LANG=zh_CN.gb2312

PROG=$1
shift
exec $PROG $*

To run XMMS, just type chstart.sh xmms in a terminal, or create a shortcut on desktop/panel to do it, or use an alias.

5.6 Instant Messengers
It was actually amazing to find out that popular instant messengers such as AMSN and Yahoo Messenger do not support Chinese. It is possible to input Chinese in YM, but difficult to make it displayed properly. AMSN simply stops taking any keyboard input when the locale is set to Chinese.

A work-around for AMSN is to use the enstart.sh to start it instead (but of course you cannot expect to input Chinese with it).

Fortunately, there are instant messengers that do support Chinese. Skype is an example, but only works when gdm/kdm has the locale set to zh_CN.gb2312 otherwise it will not work even if you start it with the chstart.h script above.

Pidgin (then GAIM) is another cute IM that was reported to work with Chinese. It is compatible with AIM, Yahoo! Messenger, MSN Messenger, Google Talk (Jabber), etc. It now even supports QQ, the most popular ICQ-like messenger in China.

5.7 Java

(This section is contributed by Guohan Lu, lguohan at gmail.com)

One of things trouble me a lot is the Java on Debian do not support Chinese natively. I got the solution to Java1.5 recently (tested for GB2312).

Here is the way (two steps):
1. make a directory fallback under JavaHome/lib/fonts/
2. add a Chinese TrueType or Type 1 font under fallback

An example:

mkdir /usr/lib/j2sdk1.5-sun/jre/lib/fonts/fallback

ln -s /usr/share/fonts/truetype/arphic/gbsn00lp.ttf .


Note the trailing dot at the end of the above line.

Some explanations for the fallback:

"... If the runtime environment has a directory lib/fonts/fallback and this directory contains valid TrueType or Type 1 fonts, the runtime automatically adds these fonts as fallback fonts for 2D rendering..."
--- from http://java.sun.com/j2se/1.5.0/docs/guide/intl/fontconfig.html

Note: This solution should be considered as quick and dirty, the best solution would be change the fontconfig file for Java. But, I haven't figured out that yet.