Building test targets in Chromium

Generally, we use 'gn gen out/Debug' and 'ninja -C out/Debug chrome' to build the chromium. Sometimes we need some unit tests to be built and run to test those changes. Mostly the unit test targets had already generated by gn gen command. So all we have to do is look for the target name and invoke it from command line.

  1. Look for the ninja build file that containing the unit test source you want to build. For example, the ninja build file containing  'base/files/' is 'obj/base/', which is driven by '' by a line 'build base$:base_unittests: phony ./base_unittests'.
  2. So, run 'ninja -C out/Debug base:base_unittests' to build the base_unittests executable file.
  3. That’s all.

Build Qt from source and install it on Windows

The major problem is its installation.

1. Suing shadow building. Configure the building in a side directory.

configure --target xp --prefix the-correct-path-to-install -D _USING_V110_SDK71_ 

For example,

configure --target xp --prefix C:/Qt/5.6.3/msvc2015_xp -D _USING_V110_SDK71_ 

The correct install path is important, since a qconfig.cpp file would be generated with this path inside.

2. nmake
3. nmake install INSTALL_ROOT=any-directory-you-want

The directory specified in INSTALL_ROOT variable is a temporary location.

4. Copy all things under INSTALL_ROOT/Qt/5.6.3/qtbase/ to your real installation directory.

5. Make a new qt.conf inside installation directory/bin

That’s all. Now you can add this new installation into Qt Creator manually.

Old Android version, linux shared library and apk packer controlled by Android Studio

  1. Old Android version (4.4 on my side) didn’t look for shared library by SONAME inside. It only checks file name. For example, in recent Android version (7 or 8), one file named whose SONAME is can be loaded by dependents successfully. But in old versions the system reported “dlopen failed to load”.
  2. In Linux’s programming conventions, a shared library usually has name like and a symbolic link named would also be created pointing to the former. That’s OK in a normal Linux system, but has problems with Android’s app.  The android apk’s zip format didn’t recognize symbolic link, and as I described in 1. it also didn’t load shared library by  SONAME.
  3. Android Studio integrated CMake to build native libraries and it also packs what generated by CMake (app/build/intemediates/cmake/<debug-or-release>/obj/<arch>)/) to libs directory inside the apk. That’s good. But! But it packs only files with .so extension. So, files like would be packed into the apk, and we don’t have any configurable option on both gradle file and CMakefile.txt to add them manually.

Strange behavior in WOW64 DLL loading in Windows 2008 R2

That is, if a 32bit DLL was loaded by CreateRemoteThread(), the DLL would be unloaded silently without DLL_PROCESS_DETACH invocation sent to DLLMain() of the DLL. There are indeed 2 weird issues, one is silent unloading, the other is no DLL_PROCESS_DETACH callback.

This occurs on WOW64 environment of Windows 2008 Server R2 only.  Windows 2012 and 2016 don’t have this issue, while x64 environment of 2008 also doesn’t have the issue.

To fix it, we can make the DLL depend on another DLL. The 2nd DLL can receive all notification callbacks of DLLMain(). This way can fix the callback issue. And by adding an extra LoadLibrary() call of 2nd DLL in 1st DLL’s DLL_PROCESS_ATTACH notification, we can keep the 2nd DLL in memory even if 1st DLL unloaded silently.


MacPorts trick for High Sierra taster

If you happen see a lot of port failed to build and error is about clock_gettime or even u_int, it is because some definition are in platform SDK instead /usr/**/*.h

Here is the trick:

Maybe /opt/local/etc/macports.conf is the right place to config it, but I have not found the key yet.

How does __TEXT encode address in __DATA?

Session 406 of WWDC 2016 is great! It explains clearly how the iOS application is settled in memory before running, with ASLR and code-signing. The key point is the indirect pointers in __DATA avoid patching the __TEXT, which is required by ASLR but impossible because of code-signing.

There is this one point confusing me, __TEXT still needs to encode address pointing to __DATA segment, though __DATA page address is randomised now.

The answer is quite simple indeed: the offset between __TEXT and __DATA is not changing between runs, the offset is encoded in __TEXT.

In 64bit Mac OS X, it implements as:

movl 0xae(%rip), %edi

iOS ARMv7 generates:

 0000bf9c f240037c movw r3, #0x7c
 0000bfa0 f2c00300 movt r3, #0x0
 0000bfa4     447b add r3, pc  ;$pc(0xbfa8) + 0x7c -> global_var in __DATA

iOS ARMv8 (64) generates:

 0000000100007f2c adrp x8, 1 ; 0x100007000 + 4k*1
 0000000100007f30 add x8, x8, #64 ; +0x40 -> 0x100008040 
                                  ;-> global_var in __DATA

* adrp xd label  mask out lower 12bits of pc then add label<<12 to generate an address.

curl certificate issue

Mac OS X El Capitan, MacPorts, it goes well for so long. And one day, git complains:

 curl: (77) error setting certificate verify locations:
 CAfile: /opt/local/share/curl/curl-ca-bundle.crt
 CApath: none

Easy, update git config system to use ca-bundle.crt found by searching.

 sslcainfo = /opt/local/share/apps/kssl/ca-bundle.crt

Problem sovled.

But, curl itself still complains:

curl --cacert /opt/local/share/apps/kssl/ca-bundle.crt ...

Problem solved again.

But, it is hard to bear the burden of adding such a long argument!

Why did curl ask for the damn file not existed?

curl-config --ca

OK, it seems that’s the value coded in the source. Fortunately, the command will check CURL_CA_BUNDLE environment variable.
Patch the ~/.profile (or .bashrc for bash, .zshrc for zsh…)


The problem is really resolved.

Auto Layout UITableView cell resizing with multi-line UILabel issue in iOS7

There are many blogs and articles talked about this issue and resolution on Internet. But any of them has this or that problems in those solutions. So I took several hours on researches and got a perfect result, at least I think so. 🙂

Thought about device orientation change, table width and cell width will change. And since we are already use Auto Layout here, it is not a good practice to design cell with any implicit width, say, 320. in Interface Builder. Almost all solutions on Internet assumed 320 width implicitly so those solutions would fail on either landscape orientation, or, iPad if the app is a universal app and you use same cell design for both iPhone and iPad.

So, the problem is in [tableView: heightForRowAtIndexPath:] delegate method. This delegate method is required for iOS7 or the app would crash. To return a correct height value of the cell, we need those steps:

  1. Set cell width to table width after loaded the cell from xib.
  2. Set values for widgets including multi-line UILabel inside cell
  3. Call [cell setNeedsLayout] and [cell layoutIfNeeded]. After those 2 calls, system’s layout engine would set correct width for UILabels. Note at this time the label’s preferredMaxLayoutWidth is still zero or any value left from last using so the layout engine cannot wrap lines and adjust label’s frame correctly.
  4. Set label.preferredMacLayoutWidth = CGRectGetWidth(label.frame)
  5. Call [cell setNeedsLayout] and [cell layoutIfNeeded] again. This time labels have correct preferredMaxLayoutWidth and the layout engine would adjust label’s frame correctly
  6. Now you can call [systemLayoutSizeFittingSize:UILayoutFittingCompressedSize] to get correct height.

Solutions on Internet usually don’t do step 1 and 5. In an invariant screen size they can get correct display but they are not a real solution for other cases.

However, from iOS8 there is a simple solution. Apple solved the issue. Just returning UITableViewAutomaticDimension or just not implementing the method will get correct result. But for apps having to maintain compatibility with iOS7, we still have to do something like this.

国内网盘的 API 服务粗览

因为要写一个从云端读取文件的程序, 首先想到的自然就是目前各家火爆的网盘服务了. 于是今天就花了点时间, 逐个看了一下. 当然, 因为是要写程序嘛, 所以网盘是否提供 API 当然就是重点了. 除了 API 之外, 也要能方便地从浏览器访问, 这样程序才有东西可以读, 对吧.

1. 百度云网盘

原来是有开放 API 的, 叫 PCSAPI (个人云服务), 但是现在已经不支持了. 新的移动接入方式在内测中, 只支持 Android, 并且需要申请. 申请表格中需要填写申请者详细信息以及应用本身的信息和下载方式. 申请之后需要审核, 也不知道要审核多久会不会有回应. 另外, 作为一个需要从云端下载文件的应用, 在你云端都还没有让我访问之前, 我怎么可能有应用存在并供你下载?

另外, 这个 android 的 SDK 还需要你在手机上安装了百度云的 app 才能工作.

2. 360 网盘

不支持 API.

3. 微盘

微盘有公开 API 支持, 开发者简单创建应用后即可使用. 很好. 但是微盘有个应用间微盘空间隔离的策略. 也就是任一应用都不能访问别的应用的存储空间, 也不能访问在 web 上访问到的微盘内容. 从 web 微盘页面上, 也看不到应用的存储空间. 这对于要从 web 传文件而应用只是下载的情形就不行了. 当然, 微盘也还是有可以访问全部空间的选择项的, 只不过也需要申请并审核. 不知道要审核多久会不会有回应.

4. 华为网盘

华为 dbank 网盘做得其实也挺早的, 然而华为做云服务好象总是做不好. 目前华为网盘的注册登录界面到处都有 connection reset 的错误, 连正常的注册登录都没法完成, 让人觉得是不是已经没人维护, 准备抛弃了.

5. 腾讯微云

腾讯微云的 API 是夹杂在一堆无关的 QQ API 中间的. 最要命的是, 腾讯注册开发者需要你上传 “手持身份证” 的照片. Why? 实名登记以便跨省?

6. 115 网盘

115 曾经名声响亮. 然而, 打开 115 首页, 赫然要求只能用 115 浏览器登录, 或者用 115 手机 app 扫码才能登录. 我勒个去, 吃相也未免太太太难看了吧?

7. 金山快盘

注册用户时报错 “服务器正在维护中”, 用个第三方帐号登录进去, 对网盘的页面随便做什么操作都会显示 “服务维护中” 的信息. 同样让人觉得是不是没人维护即将关闭了.

8. 酷盘


9. 千易

只支持网页中嵌入上传文件到千易, 以及为上传的文件提供一个下载链接. 没有 API.

10. 七牛

七牛的 API 比较完整. 然而它不是做网盘的, 它是做云存储服务的. 它没有面向普通用户的 web 网盘操作界面.


好象想不出来别的网盘服务了. 国内比较流行知名的面向普通用户的网盘也就这些了吧, 然而, 结果是, 没有一个能够提供方便的 API 供第三方应用使用. 这其实正是你国互联网界的一个缩影, 外表光鲜, 热闹非凡, 内里却都是破棉絮.

Odd behavior in Windows Service status reporting from recent Windows 2008 R2

I have a Windows service program written many year ago and the user asked me to fix an issue that occurs recently in Windows 2008 R2. Indeed it is not a big problem but quite annoying. When the user stops the service, Windows always popup a window showing an error message like “Windows could not stop the […] service on local computer, The service did not return an error.” Damn it! How could you show me an error dialog with no error message?

Google doesn’t have any similar case like this. So I have try this and that, here and there.

OK, everybody knows that kind of experiments are boring and time consuming. So I just expose the answer.

In the service’s control handler codes for SERVICE_CONTROL_STOP, I reported a SERVICE_RUNNING status followed by a SERVICE_STOP status. That works without any problem before, but caused the issue in recent 2008 R2. After I changed the first status reporting to SERVICE_STOP_PENDING, the dialog disappears.

It looks like Windows doesn’t accept SERVICE_RUNNING when it sent to services SERVICE_CONTROL_STOP. But can you show me a clear error message?