Pyridine and pyrimidine compounds according to formula Iwherein R1, R2, R3, R4, R5, X1 and A are as described herein which inhibit Btk. The compounds disclosed herein are useful to modulate the activity of Btk and treat diseases associated with excessive Btk activity. The compounds are further useful to treat inflammatory and auto immune diseases associated with aberrant B-cell proliferation such as rheumatoid arthritis. Also disclosed are compositions containing compounds of formula I and at least one carrier, diluent or excipient.