use build constraints

use build constraints to remove duplicated code.