Install/Configure Jenkins continuous integration server on Linux.

In this blog post I will describe installing and configuring Jenkins Continuous integration server. This set-up was used for one of  my current project to build Linux Application software.

I used Ubuntu 12.04 virtual machine running inside VirtualBox for this tutorial.

Jenkins is an continuous integration server written Java . Jenkins monitor configuration management ( CVS,SVN,Perforce.. ) servers  for changes , such as source code check-ins. Once it detects changes it will update the local working directory with the source from CM and performs series of build steps defined by the user on the source code. These build steps could be simple as invoking the shell script or  build script such as make,ant .

Jenkins has many plug-ins available to extend its feature set and to integrate with other Software tools ( unit test, code coverage, code analysis ).

Jenkins is Java based software , hence it requires Java Runtime as prerequisite on a system.

Jenkins software is available as Debian package . To Install latest Jenkins software on Ubuntu  execute following steps from command line

image

Once the installation completes make sure that Jenkins server up and running by opening web browser and pointing to http://localhost:8080 .

If Jenkins running you should see following page in the web browser . This completes the installation.

image

Configuring Jenkins  with Perforce Configuration Management.

Jenkins natively support CVS and SVN CM. In my application I use Perforce for configuration management. Since Jenkins natively does not support Perforce CM I need to install Perforce plugin for Jenkins.   Installing plugin is easy, Select “Jenkins->Manage Jenkins” option from the menu , select available tab to list the plugin and select and install perforce plugin. Following picture shows the “Manage Jenkins” page.

Jenkins-inst-4-perforce

You can discover and install additional plug-in similar to perforce plugin from the page.

Configure source code build.

From the Jenkins main page select NewJob to create simple build step.

In the next page enter job name and select “Build free style software project” option . You can read about the various option by selecting help icon next to option.

Jenkins-inst-4-perforce-1

Next page allows user to enter Configuration management specific details, in case Perforce CM, it user name,password and server details as shown below. These options differ based on the selected Configuration management .

Jenkins-inst-4-perforce-2

Other configuration details such as source code depot ( Perforce specific ), workspace details , Poll interval ( how often Jenkins should poll CM for changes ), where to copy the build output   are entered on the same page . Once the Configuration Management specific details are entered next step is provide instruction Jenkins to build software.

For this demo I have selected option to “Execute Shell” option to enter build command. With this option you can enter any Linux shell command . I will Apache ant to build my source code . Jenkins will execute these command once it check-out source code from the CM . I have entered following command to illustrate the build setup

cd projecta-src/

echo “Build started…”

ant BuildRelease

echo “Build Ended”

Jenkins-inst-4-perforce-4

After saving this step Jenkins will start monitoring  Configuration Management for changes, if changes detected it will pull changes to local workspace and  execute the build scripts.

Resources:

1. Jenkins website.

2. Jenkins Plugin repository.

Embedded Linux application development

Currently I am working on project which is uses custom version of embedded Linux built using open embedded framework. This project also uses QT for all the UI development ,googletest for c++ unit test and boost c++ for all threading and asynchronous communication(boost.asio).

As part of development I have explored and collected web resources which helped me to explore ,understand and tryout the samples and tools. I thought  I would share the same in this blog post.

Boost C++ :

  1. The Boost C++ Libraries  : Excellent introduction Boost C++ libraries. It covers many boost libraries such smart pointers,function objects,boost.bind,boost asio,boost threads,boost file system and many more. It includes lot of examples on each topic.
  2. Guide to Boost asynchronous I/O :  Boost.asio is a library to develop responsive applications using  async I/O facilities built into moderns OS. It is built on top of the Operating System specific asynchronous IO facilities such as IO Completion ports on windows and  epoll and similar on UNIX based Operating systems. This tutorials covers  boost asio library in depth . It has numerous examples to illustrate the concepts used in library.
  3. Asio C++ Library : This tutorial is from the developer of boost.asio library. It covers tips and tricks and sample code to illustrate how to use boost.asio to execute arbitrary tasks using thread pool and asynchronous facilities.

QT :

  1. C++ QT training videos on YouTube :  Total 106 videos which cover QT basic to advanced concepts.
  2. QT Videos at QT project site : Collection screencast which covers wide range of QT topics from beginners to advanced presented by top QT training firms such as KDAB and ICSNetwork.

 

Embedded Linux

  1. Free Electrons :  One of best site for Linux and Embedded Linux. Lot of great free training materials.
  2. Embedded Linux conference video archive : Embedded Linux Conference is as vendor-neutral technical conference for companies and developers using Linux in embedded products.  This website has all session recordings of a conference from 2005 to till date .

googletest :

  1. Quick Intro to Google C++ Test framework : This article from IBM developer works technical library, gently introduces google test framework.
  2. Google C++ framework wiki :

C++ IDE

  1. C/C++ Remote development using Netbeans  : This is great article which explains how to setup Netbeans IDE for C/C++ development . It explains how develop/debug application on Windows targeting Linux Box. It uses gcc tools /gnu debugger  over SSH/Telnet.

Programming in modern C++ with C++ 11

If you are following C++ lately you might have herd of C++ 11. C++ 11 is new revision to C++ standard approved in 2011. It offers significant improvements in both the language and the library. It include features such lambda functions/expression, range based for loop, uniform initialization, automatic type deduction, variadic templates and many more.
On the library side changes include regular expressions, atomic operations, threading (futures and promises), and tuples. Even though some of these feature exits for while in boost library now it is part of C++ standard hence it will be available from all compiler vendors (GCC,VC++ and CLANG).

With these improvements , programming in C++ 11 is as easy as in Java or C#.

In this blog post I will cover some of features I have explored in C++11.

1. Automatic Type deduction with ‘auto’

With C++ 11 , you can declare variable or object without specifying its specific type by using ‘auto’ keyword. This is especially useful if you are working with templates or lambda functions where actual type can be hard to write but compiler can infer from the expression. This feature is similar to c# var keyword

auto count  = 10; // compiler infer count type has int

vector wordCount = { 10,20,30,40 };

auto pos = wordCount.begin() ; //  compiler infer pos has vector::iteraror type.

// compiler infer the squareFunction is lambda function with integer input and integer return type.
auto squareFunction = [] (int x) -> int
                      { return x*x; };

// other example is iterating over any C++ containers using ranged based for loop
for(auto wordC : wordCount )
{
   cout<<wordC<<endl;

   cout<<squareFunction(wordC)<<endl;
}

2. Uniform initialization and Initializer list

This feature allows initialize any type (custom or built-in) using single common syntax. Following examples shows initializing built-in primitive types, containers and custom object using braces syntax

int count = { 10 };

double tempratureRange []  = {32.7,46.9,40.6,67.1,12.8 };

vector days = { "sunday","monday","tuesday",
                        "wednesday","thursday","friday","saturday"
                      };
map<string,vector> phoneList = { {"Joe",{"222-456-234","222-111-555"}},
                                         {"John",{"444-656-345","111-123-567"}}
                                        } ;
CustomType ct = {1,2,3,4,5,6};

for(auto day : days )
{
   cout<<day<<endl;
}

for(auto & phone : phoneList )
{
  cout<<phone.first<<endl;
  for(auto & num : phone.second )
       cout<<num<<" , ";
  cout<<endl;
}

3. Lambda functions/expression

This feature allows creating anonymous functions that can be passed/returned from another function. This feature used along with C++ generic function/algorithms allows writing short snippet of code inline to perform operation on C++ containers.

[] {
     cout<<"Hello from lambda"<<endl; }(); // square function auto squareFunction = [] (int x) -> int
                         { return x*x; };

vector values = {1,2,3,4,5};

for_each(values.begin(),values.end() , [&values] ( int n ) {
                                       cout<<n;
                                      if ( n % 2 == 0 )
                                         cout<<" is even number"<<endl;
                                      else
                                         cout<<" is odd numnber"<<endl;
        });

4. Range based for loops

This feature makes iterating over collection using simple syntax. It works with all standard containers

vector days = { "sunday","monday","tuesday",
                        "wednesday","thursday","friday",
                        "saturday"};
map<string,vector> phoneList = { {"Joe",{"222-456-234","222-111-555"}},
                                         {"John",{"444-656-345","111-123-567"}}
                                        } ;
// iterating over vector
for(auto day : days )
{
   cout<<day<<endl;
}

// iterating over map
for(auto & phone : phoneList )
{
    cout<<phone.first<<endl;
    for(auto & num : phone.second )
        cout<<num<<" , ";
    cout<<endl;
}

5. Variadic templates.

This feature allows users to write template class or function that can take variable number of types. For example writing type safe printf style function: See Wikipedia article . This feature will be more useful for library authors. Tuples are implemented using variadic templates as tuple can take multiple arguments of various types.

6. Tuples and Raw string literals

Tuples are ordered list of elements, many of the functional programming languages have this feature. Basically it allows to pass/return multiple values to/from function as single unit without creating separate custom type to do the same.

Example: to parse tcp host details to separate hostname and port.

tuple<string,string> ParseTcpHostDetails(const string & tcpHost)
{
   string host = tcpHost.substr(0,tcpHost.find_first_of(':'));
   string port = tcpHost.substr(tcpHost.find_first_of(':')+1);
   return make_tuple(host,port);
}
int main(int argc,char* argv[])
{
   auto parsedValue = ParseTcpHostDetails("127.0.0.1:9090");
   string host = std::get(parsedValue);
   string port = std::get(parsedValue);
}

Raw string literals allows to write character strings where escape character are not processed. A raw string literal starts with R”( and ends in )”

string uncPath = "\\\\servername\\sharepath";
string uncPathWithRawStringLiterals = R"(\\servername\sharepath)";

7.  Threading support ( async,futures and promises )

These features collectively help in writing concurrent application in a standard way on different OS platforms.

async allows launching task that can be executed on background thread, future allows waiting for the task to complete and wait for the result

int main(int argc ,char* arv[])
{
  std::future result1(std::async( [] () {
     cout<<"["<<std::this_thread::get_id()<<"] Inside long running task .. "<<endl;
  }));

  cout<<result1.get()<<endl;
}

All samples programs are compiled on Ubuntu 12.10 with GCC 4.7.2 compiler with -std=c++0x switch.

// sample c++ file for compilation with proper header include
// use 'g++ -g -std=c++0x -o Sample Sample.cxx' compiler options
#include
#include
#include
<map>
#include
#include
#include
using namespace std;
int main(int argc,char * argv[])
{

return 0;
}

In future blog posts I will write about remaining C++ 11 features such as

  • Decltypes
  • Move constructor & Delegating Constructors
  • Move semantics
  • New ways of making a Class non copy able and assignable using keyword
  • New smart pointer classes
  • More thread/async/future/promise examples

Resources

1. isocpp website provides all the news , articles ,videos and blogs related to C++.

2. C++ 11 FAQ by Bjarne Stroustrup.

3. podcast :Excellent C++ 11 podcast on software engineering radio with Scott Meyers.

4. Future of C++ : Build 2012 Talk by Herb Sutter.

C# 5 Callerinfo Attribute

As application developer we log information related to execution (information or error) to log files. This information is helpful for tracing, debugging, and creating diagnostic tools. If the log message has file name, line number and function name it is easy to narrow down the issue file and function.

In C/C++ developer can use  __FILE__ , __LINE__ and __FUNCTION__ macros to print file name, function name and line information along with the log message.

Following code c++ code illustrate the usage

// logger function
void LogMessage(std::string caller ,std::string LogMessage)
{
	std::cout<<__FILE__<<":"<<__LINE__<<" - "<<caller<<" "<<LogMessage<<std::endl;
}
// main function
int main(int argc, char* argv[])
{
	LogMessage (__FUNCTION__,"Starting Application");
	return 0;
}

If you are .Net developer  prior to .Net 4.0  you would have used System.Diagnostics.StackFrameclass to get this information .

public class Program
{
	public static void LogMessage(String message)
	{
		// get access to caller stackframe
		StackFrame frame = new System.Diagnostics.StackTrace(true).GetFrame(1);
		String file = frame.GetFileName();
		int line = frame.GetFileLineNumber();
		String member = frame.GetMethod().Name;
		
		var s = String.Format("{0}:{1} - {2}: {3}", file, line, member, message);
		Console.WriteLine(s);
	}
	static void Main(string[] args)
	{
		LogMessage("Starting the application ...");
	}
}

In C# 5 and VB.Net 11 there is easier way of getting this information using CallerInfo attribute.By using Caller Info attributes, you can obtain information about the caller to a method.

You can obtain file path of the source code, the line number in the source code, and the member name of the caller.

Here is the sample which uses C#5 feature ( available in .Net 4.5 )

public class Program
{
	public static void LogMessage(    
	string message,
	[CallerFilePath] string file = "",
	[CallerLineNumber] int line = 0,
	[CallerMemberName] string member = "")
	{
		var s = string.Format("{0}:{1} - {2}: {3}", file, line, member, message);
		Console.WriteLine(s);
	}
	static void Main(string[] args)
	{
		LogMessage("Starting the application ...");
	}
}

More information about Callinfo attribute can be found here
1. MSDN

Microsoft All-In-One Code Framework

Often you will looking for code samples to solve certain programing tasks.
Code samples will help in  understanding available operating system API’s and it usage.

All-In-One Code Framework  is code samples library provided by Microsoft. More than 3500 quality code samples covering various technical topic ranging from System programming,Device Driver programming, Windows Azure,ASP.NET ,Office SharePoint etc. Samples will be available both for Native ( C++,MFC ) and .NET developers.

Microsoft All-In-One Code Framework  has “Sample browser” application , that helps you to find the code samples written by the Microsoft team.
These sample are based on request from developer communities, customers and typical  programming scenarios.

image[3]-20[1]

 

To find the code samples
Visit http://1code.codeplex.com/ and download the application. This application is based on click once technology.

Once it is downloaded , you can launch the application and search for samples by entering the query in the search box.
You can also apply search filter based on programming language ( C++,C#,VB.NET ) and technology.
Following picture shows the search results for “Mutex” with C++ filter.

 

allinone

 

More information can be found from

1. Codeplex website

2. Channel9 Resource

3. How to use video on developer channel

Embedding C++ library/exe inside .NET assembly

Often times in .NET you have to use your existing C++ library to get work done. Traditional way of doing is using Platform invoke API to access un-managed code and distribute both unmanaged library and managed code to end user. For simple applications shipping separate .NET executable and corresponding library can be cumbersome. You will lose simplicity of deployment.

Following example shows the techniques of single file deployment using binary data as resource.

.NET allows embedding any data (binary/text) as resource inside the assembly, using this technique you can create single file assembly which houses any number of binary data.

As an example, let say I have C++ library (TestLibrary.dll) which I need to use from my .NET application (MyApplication).

1. Create new .NET application MyApplication.

2. Add existing item “TestLibrary.dll” to the MyApplication. Select the properties windows by selecting “TestLibrary.DLL” in the solution explorer. Set “Embedded Resource” option for the “Build Action” property. This option enables to embed Test.dll as part of the MyApplication assembly.

3. From MyApplication main application extra DLL resource to a temporary file.

4. Load the temporary DLL to process using Win32 native Load Library function.

5. Use DLL functions from the .NET application using PInvoke.

Advantages.

1. Deployment is easier with single file executable.

2. If needed you can embed unmanaged executable inside .NET assembly and invoke from .NET using Process Start API.

3. You can invoke different executable based on the target OS. For example: By embedding both 32bit/64bit unmanaged executable , you pick executable to launch based on the target OS,

Disadvantages:

1. .NET Application size increases based on the number of resources.

2. If you want to change embedded resource you need to rebuild the host application.

Note: Similar technique can be used to embed other .NET assembly. See Jeffrey Richter Presentation

Following sample shows the how to extract unmanaged DLL from  the resource and invoke function present in unmanaged DLL.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace MyApplication
{
    static public class NativeMethods
    {
        [DllImport("kernel32.dll")]
        public static extern IntPtr LoadLibrary(string dllToLoad);


        [DllImport("TestLibrary.dll", 
        CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr GetString();
    }

    public static class CommonUtils
    {
        /// <summary>
        /// Loads given unmanaged library from the assembly.
        /// This allows to embed .dll file as resource inside assembly.
        /// Example: 
        /// string tempDllPath = LoadUnmanagedLibraryFromResource( Assembly.GetExecutingAssembly(),
        ///                                                         "PInvokeTest.TestLibrary.dll", 
        ///                                                         "TestLibrary.dll");
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="libraryResourceName"></param>
        /// <param name="libraryName"></param>
        /// <returns></returns>
        public static string LoadUnmanagedLibraryFromResource(Assembly assembly, 
            string libraryResourceName, 
            string libraryName)
        {
            string tempDllPath = string.Empty;
            using (Stream s = assembly.GetManifestResourceStream(libraryResourceName))
            {
                byte[] data = new BinaryReader(s).ReadBytes((int)s.Length);

                string assemblyPath = Path.GetDirectoryName(assembly.Location);
                tempDllPath = Path.Combine(assemblyPath, libraryName);
                File.WriteAllBytes(tempDllPath, data);

            }

            NativeMethods.LoadLibrary(libraryName);
            return tempDllPath;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            string resourceName = "MyApplication.TestLibrary.dll";
            string libraryName = "TestLibrary.dll";

            // create and load library from the resource
            string tempDllPath = CommonUtils.LoadUnmanagedLibraryFromResource(Assembly.GetExecutingAssembly(),
                resourceName, 
                libraryName);
            // invoke native library function
            string output = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(NativeMethods.GetString());

            Console.WriteLine(output);
            
        }
    }
}

Yesterday at build conference Microsoft released details about Windows 8. Windows 8 is available on Intel and ARM platform.

For developer they are lot of interesting capabilities. Till now if you had to develop windows desktop application , you had to learn C++ ( win32,mfc) .C# or VB.  In windows 8 that capability has expended to all web developer who knows html  JavaScript and css.

Here is how developer stack looks. All the capability is of windows are exposed via WinRT API ( brand new runtime ) , programming environment is built on top of the WinRT API, so whether you are C++ developer or JavaScript developer you will get the same capability .

You can watch the walkthrough video via channel 9 link

http://channel9.msdn.com/posts/Jensen-Harris-Walks-Us-Through-the-Windows-8-UI

Also you can download windows 8 and check yourself. Installation does not requires any activation and ISO images comes with the developer tools to developer application on windows 8.

http://msdn.microsoft.com/en-us/windows/apps/br229516